0

在 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);
4

1 回答 1

0

You can concat geojson objects before add to map.

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

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

Or, just use leaflet.js geojson addData

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

(Fiddle)

于 2018-01-23T03:17:01.257 回答