_leaflet_id returns onEachFeature 函数内未定义
_leaflet_id returns undefined inside onEachFeature function
我尝试通过 function onEachFeature
访问每个 feature
的 _leaflet_id
。它总是 returns undefined
,当与它一起使用时:
function onEachFeature(feature, layer) {
console.log(layer._leaflet_id);
}
我在使用时得到了正确的 _leaflet_id
:
sampleGeoJSON.eachLayer(function(layer) {
console.log(layer._leaflet_id);
})
_leaflet_id
是Leaflet库在需要时设置的内部标识符。
当您第一次构建图层时(在您的例子中,我猜是通过 L.geoJSON
工厂?),它们可能还没有那个标识符。
如果你想强制 Leaflet 分配标识符,请使用 L.Util.stamp
(或快捷方式 L.stamp
),如果 none 已设置。
我尝试通过 function onEachFeature
访问每个 feature
的 _leaflet_id
。它总是 returns undefined
,当与它一起使用时:
function onEachFeature(feature, layer) {
console.log(layer._leaflet_id);
}
我在使用时得到了正确的 _leaflet_id
:
sampleGeoJSON.eachLayer(function(layer) {
console.log(layer._leaflet_id);
})
_leaflet_id
是Leaflet库在需要时设置的内部标识符。
当您第一次构建图层时(在您的例子中,我猜是通过 L.geoJSON
工厂?),它们可能还没有那个标识符。
如果你想强制 Leaflet 分配标识符,请使用 L.Util.stamp
(或快捷方式 L.stamp
),如果 none 已设置。