Come aggiornare / ricaricare dinamicamente un livello KML in OpenLayers


9

In precedenza ho posto questa domanda inutilmente, quindi ho pensato di metterlo qui sarebbe una buona idea.

Ecco un esempio su:

Come aggiornare / ricaricare un livello KML in OpenLayers. Livello KML dinamico. Vedi la mia risposta qui sotto.


TLDR: vedi la mia risposta di seguito.

Risposte:


7

Pensato visto che è stato abbastanza difficile per me trovare informazioni su questo aggiungerei questo:


1)

Crea il livello 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)

Imposta l'URL per il livello KML:

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

3)

Imposta l'intervallo in cui aggiornare il livello:

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

4)

La funzione per aggiornare il livello:

            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 -//
            }

Spero che questo renda più facile per alcuni altri là fuori. In bocca al lupo.


Cosa succede se il tuo KML è locale o su un'unità di rete?
Furlong,

2

Sono stato in giro con questo inutilmente. Qualcuno potrebbe dare un'occhiata al mio codice e dirmi cosa sto facendo di sbagliato? Grazie!

<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>

Mi dispiace, ho dovuto correggere i tuoi codici imperfetti. Dovrebbe funzionare questa volta.



0

non testato ma qualcosa del genere?

<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>
Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.