Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to loop through geojson layers on leaflet map?

Tags:

leaflet

I have already added geojson features to my leaflet map. I want to be able to loop through those geojson features. WHen I do map.eachLayer(function(layer) {...}) it only shows be the tile layer and none of the geojson that are added.

like image 962
Rolando Avatar asked Apr 07 '16 03:04

Rolando


1 Answers

Rather than map.eachLayer, you should be using the .eachLayer method on the L.geoJson itself. For example:

var geoJsonLayer = L.geoJson(myGeoJson).addTo(map);

geoJsonLayer.eachLayer(function(layer) {
  layer.bindPopup(layer.feature.properties.name);
});

You can also specify a function to be applied to each feature at the time you create the L.geoJson, using the onEachFeature option.

like image 96
nathansnider Avatar answered Oct 15 '22 02:10

nathansnider