问题描述
我正在尝试检查每个geojson功能是否为标记。如果要删除放置的图层,请再次初始化绘图标记。
如果位置不同,我将其添加到要素图层中。
问题在于eachLayer始终返回true,因为它遍历所有图层,并且始终返回true,因为标记已添加到要素中。所以它总是重复。
const num = 1115487.2644548;
const len = String(parseInt(num)).length
const two = len-2;
console.log(num/10**two); // without testing the len > 2
这是摆弄,我很忘了添加重要的代码。 https://jsfiddle.net/2ftmy0bu/2/
解决方法
将您的代码更改为:
// listen to when a new layer is created
map.on('pm:create',function(e) {
//should only place one marker each time
// check if the layer with the same latlng exists
var eq = features.getLayers().find(function(layer){
if(layer instanceof L.Marker) {
return layer.getLatLng().equals(e.layer.getLatLng())
}else{
return false;
}
}) !== undefined;
if(!eq) {
console.log('not equal')
features.addLayer(e.layer);
map.pm.disableDraw('Marker')
//if marker is placed on the map and it is not placed on same place as another marker
} else if(eq) {
console.log('equal')
//if marker is placed on the map over another marker,remove marker,then init draw marker again.
map.removeLayer(e.layer);
map.pm.enableDraw('Marker',{
snappable: true,snapDistance: 20,finishOn: 'click' || 'mousedown',});
// TODO: I think you don't want this
// features.addLayer(e.layer);
}
});