首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OpenLayers:显示远程kml

OpenLayers:显示远程kml
EN

Stack Overflow用户
提问于 2012-11-13 16:09:11
回答 1查看 863关注 0票数 2

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

我用我发现的东西改进了它。这就是看起来的样子。我觉得我漏掉了一些显而易见的东西。任何指针都欢迎。

代码语言:javascript
复制
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()} });
}
EN

回答 1

Stack Overflow用户

发布于 2012-11-14 09:11:56

这是一个示例,说明如何在OpenLayers中显示KML层,这可能有助于您:

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

打开页面时是否有任何错误?或者它运行正常,但什么也没有出现?如果您没有收到任何错误,那么它可能表明您的投影是如何设置的(也就是说,您的功能可能不会出现在您预期的位置)。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13364279

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档