Networking OpenLayers:显示远程kml

Networking OpenLayers:显示远程kml,networking,dynamic,openlayers,kml,Networking,Dynamic,Openlayers,Kml,我试图让OpenLayers显示从服务器检索到的KML文件。 出于某种原因,这不起作用。 有人问过类似的问题,但我找不到一个有效的例子。 我所做的是改进OpenLayers发行版中的一个示例:kml-track.js 我用我的发现改进了它。这就是它看起来的样子。我觉得我错过了一些明显的东西。 欢迎任何指点 这是一个如何在OpenLayers中显示KML层的示例,它可能会帮助您: 打开页面时是否有任何错误?或者页面运行正常但未显示任何内容?如果您没有收到任何错误,则可能表明您的投影设置存在问题

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

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




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


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

您说它不起作用。它根本不显示吗?如果签入javascript调试控制台,是否请求KML文件?
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()} });
}