Openlayers - 如何从多个KML文件导入地标

时间:2015-08-22 19:13:37

标签: arrays openlayers kml

我在各种KML文件中都有很多POI,我需要在Openlayers中导入这些文件。地图。 每个文件的地标都应该有自己的可显示层。

任何人都可以告诉我如何使用数组来收集非特定数量的KML文件。

这是我的代码,导入一个KML文件:

map = new OpenLayers.Map("mapdiv");
map.addLayer(new OpenLayers.Layer.OSM("Local Tiles", "tiles/${z}/${x}/${y}.png", {numZoomLevels: 19, alpha: true, isBaseLayer: true}));

var kml = new OpenLayers.Layer.Vector("KML", {
        strategies: [new OpenLayers.Strategy.Fixed()],
        protocol: new OpenLayers.Protocol.HTTP({
            url: "kml/test.kml",
            format: new OpenLayers.Format.KML({
                extractStyles: true, 
                extractAttributes: true,
                maxDepth: 2
            })
        })
    });


map.addLayer(kml);


var lonLat = new OpenLayers.LonLat( 7.21495 ,50.54819 )
      .transform(
        new OpenLayers.Projection("EPSG:4326"), 
        map.getProjectionObject() 
      );
var zoom=17;
map.setCenter (lonLat, zoom);  





var controls = {
  selector: new OpenLayers.Control.SelectFeature(kml, { onSelect: createPopup, onUnselect: destroyPopup })
};

function createPopup(feature) {
  feature.popup = new OpenLayers.Popup.FramedCloud("pop",
      feature.geometry.getBounds().getCenterLonLat(),
      null,
      '<div class="markerContent">'+feature.attributes.description+'</div>',
      null,
      true,
      function() { controls['selector'].unselectAll(); }
  );

  map.addPopup(feature.popup);
}

function destroyPopup(feature) {
  feature.popup.destroy();
  feature.popup = null;
}

map.addControl(controls['selector']);
controls['selector'].activate();

1 个答案:

答案 0 :(得分:1)

这是一个简单的示例,展示如何将(或“map”)任意URL数组转换为KML图层数组,然后可以使用{{1}一次性将其添加到OpenLayers地图中方法:

addLayers