从半径范围内的多个JSON图层中选择标记

时间:2019-09-09 09:47:59

标签: javascript json leaflet

我想至少合并2个json图层,以便在单击后可以在一定半径内研究所有标记。

有两个文件,在代码中看起来像这样:

var url = "Peterborough.json";
var url2 = "Test.json";

然后代码看起来像在此链接下的示例中一样:

https://gis.stackexchange.com/questions/334170/leaflet-select-markers-from-multiple-json-files-within-the-radius-given/334192#334192

直到现在,我们必须设置SelectPoints(lat,lon)函数。

然后我做了这样的事情:

enter image description here

function SelectPoints(lat,lon){
var dist = document.getElementById("miles").value;

xy = [lat,lon];  //center point of circle

var theRadius = parseInt(dist) * 1609.34  //1609.34 meters in a mile 
//dist is a string so it's convered to an Interger.

selPts.length =0;  //Reset the array if selecting new points

job.eachLayer(function (layer) {
// Lat, long of current point as it loops through.
layer_lat_long = layer.getLatLng();

// Distance from our circle marker To current point in meters
distance_from_centerPoint = layer_lat_long.distanceTo(xy);

// See if meters is within radius, add the to array
if (distance_from_centerPoint <= theRadius) {
     selPts.push(layer.feature);  
}

job2.eachLayer(function (layer) {
// Lat, long of current point as it loops through.
layer_lat_long = layer.getLatLng();

// Distance from our circle marker To current point in meters
distance_from_centerPoint = layer_lat_long.distanceTo(xy);

// See if meters is within radius, add the to array
if (distance_from_centerPoint <= theRadius) {
     selPts.push(layer.feature);  
}

});

但不幸的是,我得到了一个空白(甚至地图消失了)。

这有什么问题吗?

我在这里对这个函数的迭代有红色:

Leaflet eachLayer function does not iterate through all Layers

但是它并没有给我带来明确的解决方案。

我希望在单击标记后可以选择这2个层,而不是仅1个。

当我使用第二个JSON文件作为图层并复制

$.getJSON(url2, function(data) {

代码,然后我会在地图上清楚看到所有内容,但是单击后未选中它们。

可以澄清吗?

1 个答案:

答案 0 :(得分:0)

好像我将;放在错误的位置。

代码应如下所示:

 job.eachLayer(function (layer) {
    // Lat, long of current point as it loops through.
    layer_lat_long = layer.getLatLng();

    // Distance from our circle marker To current point in meters
    distance_from_centerPoint = layer_lat_long.distanceTo(xy);

    // See if meters is within radius, add the to array
    if (distance_from_centerPoint <= theRadius) {
         selPts.push(layer.feature);  
    }
  })

job2.eachLayer(function (layer) {
    // Lat, long of current point as it loops through.
    layer_lat_long = layer.getLatLng();

    // Distance from our circle marker To current point in meters
    distance_from_centerPoint = layer_lat_long.distanceTo(xy);

    // See if meters is within radius, add the to array
    if (distance_from_centerPoint <= theRadius) {
         selPts.push(layer.feature);  
    }
  })

根据示例:

http://www.gistechsolutions.com/leaflet/DEMO/sports/