0

我正在尝试检查每个 geojson 功能是否是标记。如果是我想删除放置的图层,然后再次初始化绘图标记。

如果不是同一个位置,我就把它加到要素图层上。

问题在于 eachLayer 它总是返回 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);
    }
  }
})

这是小提琴,我忘了添加重要代码。 https://jsfiddle.net/2ftmy0bu/2/

4

1 回答 1

0

将您的代码更改为:

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

https://jsfiddle.net/falkedesign/c6Lf758j/

于 2020-10-12T12:13:16.780 回答