JS Leaflet: How do I get the (Geo-) json ID before the click event?

My django web application should do the following: pass the Geojson object to the view, display the elevator points and display additional information when the user clicks on the point marker. I am not very familiar with js, so I got attached to the correct data type for the click event. Here is an example geojson object. How can I access the "id" with my click event?

 var geojsonFeature = {'geometry': {'type': 'MultiPoint', 'coordinates': [[4.939, 52.33], [4.9409, 52.33]] }, 'type': 'Feature', 'properties': {'id': '52','popupContent': 'id=52'} }; 

Adding a geojson object to the map.

 var gj = L.geoJson(geojsonFeature, { pointToLayer: function (feature, latlng) { return L.circleMarker(latlng, geojsonMarkerOptions); }}).addTo(map); 

And on() -click ....

 gj.on('click', function(evt) { alert(id) // well, this is where I need help }); 

NOTE. I don't want to use something like bindPopup(feature.properties.popupContent) , because I want to open a new window that brings up another django view with some extra data from the database.

+7
source share
2 answers

For everyone with a similar problem: what you want to use is the onEachFeature function. The function represents a geojson object. Using the example data above, the identifier can be obtained through feature.properties.popupContent .

 function onEachFeature(feature, layer) { layer.on('click', function (e) { alert(feature.properties.popupContent); //or alert(feature.properties.id); }); } 
+13
source

After trying the solution published above, unsuccessfully, I got this version:

 function onEachFeature(feature, layer) { layer.on('click', function (e) { alert(e.target.feature.properties.popupContent); //or alert(e.target.feature.properties.id); }); } 
+2
source

All Articles