Cara menyegarkan / memuat ulang lapisan KML secara dinamis di OpenLayers

9

Saya sebelumnya mengajukan pertanyaan ini tetapi tidak berhasil, jadi saya pikir menempatkan ini di sini adalah ide yang bagus.

Berikut ini contohnya:

Cara Refresh / Reload layer KML di OpenLayers. Lapisan KML dinamis. Lihat jawaban saya di bawah ini.


TLDR: Lihat jawaban saya di bawah ini.

sphvn
sumber

Jawaban:

7

Figured terlihat cukup sulit bagi saya untuk menemukan informasi tentang ini saya akan menambahkan ini:


1)

Buat Lapisan KML:

            //Define your KML layer
            var MyKmlLayer= new OpenLayers.Layer.Vector("This Is My KML Layer", {
                //Set your projection and strategies//
                projection: new OpenLayers.Projection("EPSG:4326"),
                strategies: [new OpenLayers.Strategy.Fixed()],
                //set the protocol with a url//
                protocol: new OpenLayers.Protocol.HTTP({
                    //set the url to your variable//
                    url: mykmlurl,
                    //format this layer as KML//
                    format: new OpenLayers.Format.KML({
                        //maxDepth is how deep it will follow network links//
                        maxDepth: 1,
                        //extract styles from the KML Layer//
                        extractStyles: true,
                        //extract attributes from the KML Layer//
                        extractAttributes: true
                    })
                })
            });

2)

Setel URL untuk Lapisan KML:

//note that I have host equal to location//   //Math.Random will stop caching//
var mykmlurl= 'http://' + host + '/KML?key=' + Math.random();

3)

Setel interval untuk menyegarkan lapisan Anda:

           //function called// //timer// //layer to refresh//
window.setInterval(UpdateKmlLayer, 5000, MyKmlLayer);

4)

Fungsi untuk memperbarui lapisan:

            function UpdateKmlLayer(layer) {
                //setting loaded to false unloads the layer//
                layer.loaded = false;
                //setting visibility to true forces a reload of the layer//
                layer.setVisibility(true);
                //the refresh will force it to get the new KML data//
                layer.refresh({ force: true, params: { 'key': Math.random()} });
                //- <3 from Thqr -//
            }

Berharap ini membuat beberapa orang lebih mudah di luar sana. Semoga berhasil.

sphvn
sumber
Bagaimana jika KML Anda lokal atau di drive jaringan?
Furlong
2

Saya telah membohongi dengan ini tetapi tidak berhasil. Adakah yang keberatan melihat kode saya dan mengatakan apa yang saya lakukan salah? Terima kasih!

<html>
<head>
<title>Test</title>
<link rel="stylesheet" href="http://openlayers.org/api/theme/default/style.css" type="text/css" />
<link rel="stylesheet" href="http://openlayers.org/dev/examples/style.css" type="text/css" />
<script src='http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAl9RMqSzhPUXAfeBCXOussRTQDbvAygy0cfGJr8dEMAYKf3RWNBQqP9mjKIsqTfmAlz5LOJ3Xpy5s4w'></script>
<script src="http://openlayers.org/api/OpenLayers.js"></script>
<script type="text/javascript">

  var map;

  function init() {
    // Create the map object
    map = new OpenLayers.Map('map');
    // Create a Google layer
    var gmap = new OpenLayers.Layer.Google(
        "Google Streets", // the default
        {numZoomLevels: 20}
    );
    // Add layer to map
    map.addLayer(gmap);

    map.setCenter(new OpenLayers.LonLat(-112.1161, 33.6636), 13);         
  }

  var MyKmlLayer= new OpenLayers.Layer.Vector("This Is My KML Layer", {
            //Set your projection and strategies//
            projection: new OpenLayers.Projection("EPSG:4326"),
            strategies: [new OpenLayers.Strategy.Fixed()],
            //set the protocol with a url//
            protocol: new OpenLayers.Protocol.HTTP({
                //set the url to your variable//
                url: C:/Users/person/desktop/test.kml,
                //format this layer as KML//
                format: new OpenLayers.Format.KML({
                    //maxDepth is how deep it will follow network links//
                    maxDepth: 1,
                    //extract styles from the KML Layer//
                    extractStyles: true,
                    //extract attributes from the KML Layer//
                    extractAttributes: true
                })
            })
        });

    var proposedanchorpositionurl = 'http://' + host + '/KML?key=' + Math.random();

    window.setInterval(UpdateKmlLayer, 5000, MyKmlLayer);

    function UpdateKmlLayer(layer) {
            //setting loaded to false unloads the layer//
            layer.loaded = false;
            //setting visibility to true forces a reload of the layer//
            layer.setVisibility(true);
            //the refresh will force it to get the new KML data//
            layer.refresh({ force: true, params: { 'key': Math.random()} });
            //- <3 from Thqr -//
        }
</script>
</head>
<body onload="init()">
<h1 id="title">test</h1>
<div id="map" class=""></div>
</body>
</html>

Maaf, saya harus memperbaiki kode Anda yang tidak sempurna. Harus jalankan kali ini.

Furlong
sumber
0

belum diuji tetapi sesuatu seperti ini?

<script src="http://openlayers.org/api/OpenLayers.js"></script>
<script type="text/javascript">

  var map;

  function init() {
    // Create the map object
    map = new OpenLayers.Map('map');
    // Create a Google layer
    var gmap = new OpenLayers.Layer.Google(
        "Google Streets", // the default
        {numZoomLevels: 20}
    );
    // Add layer to map
    map.addLayer(gmap);

    map.setCenter(new OpenLayers.LonLat(-112.1161, 33.6636), 13);         


  var KMLURL = 'C:/Users/person/desktop/test.kml?' + Math.random();

  var MyKmlLayer= new OpenLayers.Layer.Vector("This Is My KML Layer", {
            //Set your projection and strategies//
            projection: new OpenLayers.Projection("EPSG:4326"),
            strategies: [new OpenLayers.Strategy.Fixed()],
            //set the protocol with a url//
            protocol: new OpenLayers.Protocol.HTTP({
                //set the url to your variable//
                url: KMLURL,
                //format this layer as KML//
                format: new OpenLayers.Format.KML({
                    //maxDepth is how deep it will follow network links//
                    maxDepth: 1,
                    //extract styles from the KML Layer//
                    extractStyles: true,
                    //extract attributes from the KML Layer//
                    extractAttributes: true
                })
            })
        });

    window.setInterval(UpdateKmlLayer, 5000, MyKmlLayer);

    function UpdateKmlLayer(layer) {
            //setting loaded to false unloads the layer//
            layer.loaded = false;
            //setting visibility to true forces a reload of the layer//
            layer.setVisibility(true);
            //the refresh will force it to get the new KML data//
            layer.refresh({ force: true, params: { 'key': Math.random()} });
            //- <3 from Thqr -//
        }
 }
</script>
sphvn
sumber