删除不靠近线串的标记

Remove markers that are not in proximity to linestring

我正在用传单构建地图,我从 .geojson 获取线条和标记,我想知道是否可以删除不靠近线条的标记,如果可以,如何?

编辑:

Fiddle: https://jsfiddle.net/utsp9zh3/

在那个例子中,我如何隐藏或删除不靠近或不接触线条的 testmarker4(以及其他类似的,如果它存在的话)。

将此添加到您的代码中:


var loadcount = 0;
Test.on('data:loaded',()=>{
    loadcount++;
  if(loadcount >= 2){
        removeMarkerIfToFar(Test,LineRouteTest)
  
  }
});
LineRouteTest.on('data:loaded',()=>{
    loadcount++;
  if(loadcount >= 2){
        removeMarkerIfToFar(Test,LineRouteTest)  
  }
})

var MINIMUM_DIS = 1000; // 1km


function removeMarkerIfToFar(markerGroup, lineGroup){
    var lineLatLngs = [];
    lineGroup.eachLayer((layer)=>{
    if(layer._latlngs){ // check if no point
        lineLatLngs = lineLatLngs.concat(layer.getLatLngs());
    }
  })
  
  markerGroup.eachLayer((layer)=>{
    if(layer instanceof L.Marker){
        var remove = true;
      lineLatLngs.forEach((latlng)=>{
        if(latlng.distanceTo(layer.getLatLng()) <=  MINIMUM_DIS){
            remove = false;
        }
      })
      
      if(remove){
        layer.remove()
      }      
    }
  })
}

https://jsfiddle.net/falkedesign/8r1aw7u0/