2017-10-06 17 views
0

在leaflet.js文檔中記錄了具有多層標記的示例。我想創建一個GeoJSON格式的多邊形圖層。這可能嗎?可能使用LayerGroup進行GeoJSON Polygons,MultiLineStrings和LineStrings?

我定義爲每個GeoJSON的多邊形一個變量,叫ROUTE1,路徑2等我的.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); 

回答

0

可以Concat的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

相關問題