可以将LayerGroup用于GeoJSON多边形,MultiLineStrings和LineStrings吗?

时间:2017-10-06 20:33:12

标签: javascript dc.leaflet.js

在leaflet.js文档中,记录了一个带有标记层的示例。我想用GeoJSON格式创建一个多边形层。这可能吗?

我为每个GeoJSON多边形定义了一个变量,名为route1,route2等。我的.js文件如下所示:

var map = L.map('map', {
    center: [55.676098, 12.568337],
});

L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
    attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);

//============
//Create layer
//============

var Southroutes = new L.layerGroup([route1, route2, route3, route4, route5]);

L.geoJSON(Southroutes).addTo(map);

1 个答案:

答案 0 :(得分:0)

您可以在添加到地图之前连接geojson对象。

var features = [];
[route1, route2, route3].forEach(function(route){
  features = features.concat(route.features);
})

var Southroutes = L.geoJSON({
  "type": "FeatureCollection",
  "features": features
}).addTo(map);

或者,只需使用leaflet.js geojson addData

var Southroutes = L.geoJSON().addTo(map);
[route1, route2, route3].forEach(function(route){
  Southroutes.addData(route);
})

Fiddle