Javascript 在地图上单击几下后,传单边界无效

Javascript 在地图上单击几下后,传单边界无效,javascript,leaflet,Javascript,Leaflet,我试图使距离线为用户点击最近的点。在地图上点击几下后,我得到的边界无效, 我得到了如下错误:无法读取未定义的属性“lat”。 然后,它再次工作后,几次点击。 请帮帮我 function queryFeatures(currentPos, numResults) { var countyLayerLayer = L.featureGroup(); countyLayer.eachLayer(function(l) { // point layer

我试图使距离线为用户点击最近的点。在地图上点击几下后,我得到的边界无效, 我得到了如下错误:无法读取未定义的属性“lat”。 然后,它再次工作后,几次点击。 请帮帮我


 function queryFeatures(currentPos, numResults) {
        
      var countyLayerLayer = L.featureGroup();
        countyLayer.eachLayer(function(l) { // point layer
            
            var distance = L.latLng(currentPos).distanceTo(l.getLatLng())/1000;
            
            distances.push(distance);

        });
        
        countyLayer.eachLayer(function(l) {
            var distance = L.latLng(currentPos).distanceTo(l.getLatLng())/1000;
             /*distances.push(distance);
*/             distances.sort(function(a, b) {
            return a - b;
        });
        
            
            if(distance < distances[numResults]) {
                
                //l.bindTooltip(distance.toLocaleString() + ' km from current location.')
    map.on("popupclose", function(e) {
    if (line) {
      map.removeLayer(line);
    }
  });

          if(line) {
            map.removeLayer(line);
          }
                
                 line  = new L.polyline([currentPos, l.getLatLng()], {
                    color : 'red',
                    weight : 2,
                    opacity: 1,
                    dashArray : "5, 10"
                }).addTo(countyLayerLayer); 
                
            }

        });
        
map.flyToBounds(countyLayerLayer.getBounds(), {duration : 3, easeLinearity: .1 }); // after few clicks bounds are not getting 
     
        map.on('zoomend', function() {
          
            map.addLayer(countyLayerLayer);
        })
      
    }


函数查询功能(currentPos、numResults){
var countyLayerLayer=L.featureGroup();
countyLayer.eachLayer(函数(l){//点层
var distance=L.latLng(当前位置).distanceTo(L.getLatLng())/1000;
距离。推(距离);
});
countyLayer.eachLayer(函数(l){
var distance=L.latLng(当前位置).distanceTo(L.getLatLng())/1000;
/*距离。推(距离);
*/距离.排序(函数(a,b){
返回a-b;
});
if(距离<距离[numResults]){
//l、 bindTooltip(距离.ToLocalString()+“距离当前位置公里”)
地图上(“弹出关闭”,函数(e){
如果(行){
地图移除层(线);
}
});
如果(行){
地图移除层(线);
}
直线=新的L.多段线([currentPos,L.getLatLng()]{
颜色:“红色”,
体重:2,
不透明度:1,
dashArray:“5,10”
}).addTo(countyLayerLayer);
}
});
map.flyToBounds(countyLayerLayer.getBounds(),{duration:3,easelinerality:.1});//单击几下后,边界将无法获取
map.on('zoomend',function(){
map.addLayer(countyLayerLayer);
})
}