2

我试图让 OpenLayers 显示从服务器检索到的 KML 文件。由于某种原因,这不起作用。有人问过类似的问题,但我找不到一个可行的例子。我所做的是改进 OpenLayers 发行版中的一个示例:kml-track.js

我用我发现的东西改进了它。这就是它的样子。我觉得我错过了一些明显的东西。欢迎任何指点


var map ;

function init() {
  var mercator = new OpenLayers.Projection("EPSG:900913");
  var geographic = new OpenLayers.Projection("EPSG:4326");

  //note that I have host equal to location//   //Math.Random will stop caching//
  var mykmlurl = 'http://myserver/kml-track.kml';

  map = new OpenLayers.Map({
    div: "map",
    projection: mercator,
    layers: [
      new OpenLayers.Layer.OSM(),

      //Defiine your KML layer//
      new OpenLayers.Layer.Vector("This Is My KML Layer", {
        //Set your projection and strategies//
        projection: geographic,
        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
          })
        }),
        styleMap: new OpenLayers.StyleMap({
          "default": new OpenLayers.Style({
            graphicName: "circle",
            pointRadius: 2,
            fillOpacity: 0.5,
            fillColor: "#ffcc66",
            strokeColor: "#666633",
            strokeWidth: 1
          })
        })
      })
    ],
    center: new OpenLayers.LonLat(-93.2735, 44.8349).transform(geographic, mercator),
    zoom: 8
  });


 //function called// //timer// //layer to refresh//
  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()} });
}

4

1 回答 1

2

这是一个如何在 OpenLayers 中显示 KML 图层的示例,可能会对您有所帮助:

http://openlayers.org/dev/examples/kml-layer.html

您在打开页面时是否遇到任何错误 - 或者它运行正常但没有出现?如果您没有收到任何错误,则可能表明您的投影设置方式存在问题(即您的功能可能不会出现在您期望的位置)

于 2012-11-14T09:11:56.807 回答