3

I have a JSFiddle and I am trying to implement onEachFeature (at the bottom) of the code.

Each polygon has its own popupContent as in the onEachFeature tutorial shown on Leaflet.js.com.

However, I get stuck trying to merge together these pieces of code:

var myGeoJson = L.geoJson(myData, {
  style: style
}).addTo(map).bindPopup("test");
var popup = L.popup();


function onEachFeature(feature, layer) {
    if (feature.properties && feature.properties.popupContent) {
        layer.bindPopup(feature.properties.popupContent);
    }
}

var myGeoJson = L.geoJson(geojsonFeature, {
    onEachFeature: onEachFeature
}).addTo(map);

Ultimately, I'm just trying to pass in the features.properties.popupContent as a variable any way possible without losing my style properties (fill color, etc).

2

Try this fiddle.

You are close with the code. You can pass style and onEachFeature:

var myGeoJson = L.geoJson(myData, {
  style: style,
  onEachFeature: onEachFeature
}).addTo(map);

Also, in myData, one of the features had a property called name instead of popupContent.

  • Awesome. Thank you. And the link to your fiddle with the answer is actually jsfiddle.net/adampax/r8ktzrkz/1 as in version 1. – achen Jun 13 '17 at 21:36
  • This was very useful, thank you. How would you do it if you had an external geojson file? E.g. if you were calling filename.geojson instead of assigning it to a variable (like myData in OP's example)? – ale19 Aug 4 '17 at 19:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.