Leaflet 检查重复标记传单geoman

Leaflet 检查重复标记传单geoman,leaflet,react-leaflet,leaflet-geoman,leaflet.pm,Leaflet,React Leaflet,Leaflet Geoman,Leaflet.pm,我试图检查每个geojson特性是否是一个标记。如果是,我想删除放置的图层,然后再次初始化绘图标记 如果它不是相同的位置,我将只将其添加到要素层 问题是,对于每个图层,它总是返回true,因为它在所有图层中循环,而它总是返回true,因为标记被添加到特征中。所以它总是重复 features.eachLayer(layer => { if(layer.pm._shape === 'Marker') { if(e.layer._latlng !== layer._latlng) {

我试图检查每个geojson特性是否是一个标记。如果是,我想删除放置的图层,然后再次初始化绘图标记

如果它不是相同的位置,我将只将其添加到要素层

问题是,对于每个图层,它总是返回true,因为它在所有图层中循环,而它总是返回true,因为标记被添加到特征中。所以它总是重复

features.eachLayer(layer => {
  if(layer.pm._shape === 'Marker') {
    if(e.layer._latlng !== layer._latlng) { //This is never true, should be true if the placed marker is not placed over an existing features marker
      features.addLayer(e.layer);

    } else if(e.layer._latlng === layer._latlng) { //this elseif is always true for some reason and will loop
      map.removeLayer(e.layer)
      DrawUtil.addMarker(map, isSnapping); //Alias for pm.enableDraw.marker
      features.addLayer(e.layer);
    }
  }
})
这是小提琴,我的坏忘了添加重要的代码。 将代码更改为:

// 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);
  }   
});

我想你可以在
pm:create
事件中调用它,请添加完整的代码。还要检查一个图层是否是带有
图层实例的L.Marker
标记,以及它是否与
e.layer.getLatLng().equals(layer.getLatLng())
相同。请创建一个JSFIDLE,它可以更容易地帮助我的坏习惯。现在添加了JSFIDLE。是的,提供的代码是在pm:create中调用的