Dynamisches Aktualisieren / erneutes Laden einer KML-Ebene in OpenLayers

9

Ich habe diese Frage zuvor vergeblich gestellt, daher dachte ich, dass es eine gute Idee wäre, sie hier aufzustellen.

Hier ist ein Beispiel für:

So aktualisieren / laden Sie eine KML-Ebene in OpenLayers neu. Dynamische KML-Schicht. Siehe meine Antwort unten.


TLDR: Siehe meine Antwort unten.

sphvn
quelle

Antworten:

7

Da es schwierig genug für mich war, Informationen dazu zu finden, würde ich Folgendes hinzufügen:


1)

Erstellen Sie die KML-Ebene:

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

Legen Sie die URL für die KML-Ebene fest:

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

3)

Legen Sie das Intervall fest, in dem Ihre Ebene aktualisiert werden soll:

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

4)

Die Funktion zum Aktualisieren der Ebene:

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

Ich hoffe, das macht es einigen anderen da draußen leichter. Viel Glück.

sphvn
quelle
Was ist, wenn sich Ihre KML lokal oder auf einem Netzwerklaufwerk befindet?
Furlong
2

Ich habe vergeblich damit herumgespielt. Würde es jemandem etwas ausmachen, sich meinen Code anzusehen und mir zu sagen, was ich falsch mache? Vielen Dank!

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

Entschuldigung, ich musste Ihre unvollständigen Codes korrigieren. Sollte diesmal laufen.

Achtelmeile
quelle
0

ungetestet aber so etwas?

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