Reputation: 490
In the leaflet.js documentation an example with layers of markers is documented. I would like to create a layer of polygons in GeoJSON format. Is this possible?
I've defined a variable for each GeoJSON polygon, called route1, route2 etc. My .js file looks like this:
var map = L.map('map', {
center: [55.676098, 12.568337],
});
L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <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);
Upvotes: 0
Views: 695
Reputation: 15
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)
Upvotes: 0