JS leaflet: How to pass (Geo-)json ID to on click event?

一笑奈何 提交于 2019-12-03 08:05:20

For everyone with a similar problem: What you want to use is the onEachFeature function. The feature represents a geojson object. Using the sample data provided above the id can be accessed through feature.properties.popupContent.

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

After trying the solution posted above without success, I got this version to work:

function onEachFeature(feature, layer) {
    layer.on('click', function (e) {
        alert(e.target.feature.properties.popupContent);
        //or
        alert(e.target.feature.properties.id);
    });
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!