我在将删除函数绑定到从 kml 文件加载的每一层(多边形)时遇到问题(甚至只是从数据库中存储的坐标)。它仅适用于第一次点击 - 我点击的第一层。之后它不适用于其他图层,似乎在第一次单击后被禁用。
当我把 bindPopup 函数放在那里时,它适用于每一层,并且弹出窗口出现,或者 console.log(this) 显示每个点击层的对象详细信息。所以我不明白为什么 removeLayer 也不适用于每一层。
我想我在这里缺少一些关于javascript的知识,你能指教吗?
(KMLfile 是谷歌制作的一个文件扩展名,用于从谷歌地球应用程序中提取区域/位置/等)
加载 KML 文件的代码:
var filePath = 'path to my kml file';
var customLayer= 'colors, line weight, etc.';
var runLayer = omnivore.kml(filePath, null, customLayer).on('ready', function() {
map.fitBounds(runLayer.getBounds());
runLayer.eachLayer(function(layer) {
//layer.bindPopup(layer.feature.properties.name+" / "+this);
layer.on('click', function() {
map.removeLayer(layer); // There's the issue, itworks only on first click!
});
});
}).addTo(loadedFeatureGroup);//adding polygons to predefined group