gpt4 book ai didi

javascript - 传单:layer.getLatLng 不适用于 .eachLayer 函数

转载 作者:行者123 更新时间:2023-11-28 07:13:11 26 4
gpt4 key购买 nike

我正在尝试做一些与 Chris Essig 所做的非常相似的事情 here 。因为我需要知道在用户放下“trashMarker”的地方 20 米半径内有多少个数据点。

到目前为止我已经得到了这段代码:

// Create the FeatureGroup and add it to the map
var jsonGroup = new L.FeatureGroup();
mapOverview.addLayer(jsonGroup)

//Retrieve all data and add to map
$.each(datalistObject['idlist'], function(key, value) {
$.getJSON('http://mydata.com' + value['id'], function(data) {
textbox = value['name'];

var dataid = L.geoJson([data], {
style: function (feature) {
return feature.properties && feature.properties.style;
},
onEachFeature: onEachFeature,
pointToLayer: function (feature, latlng) {
return L.marker(latlng, {
icon: value['icon']
});
}
}).addTo(jsonGroup);

},function(xhr) { console.error(xhr); });
});

//Code to find the markers within a 20 meter radius of trashMarker
function markersInRadius() {

// Lat, long of trash marker on overview map
var trashMarkerLat_long = trashMarkerOverview.getLatLng();

// counter of the amount of markers that are within a 20 meter radius
var counter_markers_in_radius = 0;

console.log(jsonGroup);

// Loop through each point in JSON file
jsonGroup.eachLayer(function (layer) {

// Lat, long of current point
layerLatLong = layer.getLatLng();

// Distance from our circle marker
// To current point in meters
distance_from_layer_circle = layerLatLong.distanceTo(trashMarker_lat_long);

// See if meters is within raduis
// The user has selected
if (distance_from_layer_circle <= 20) {
counter_markers_in_radius += 1;
};

console.log(counter_markers_in_radius);
});
// Close pointsInCircle
};

当我运行此代码时,收到错误消息,指出 layer.getLatLng 不是函数。

在 jsonGroup 功能组上执行 console.log 后,我发现该组的图层选项卡中有两个对象,没有任何经纬度信息,而是有一个自己的图层选项卡,其中包含所有数据点latlng 信息...也许这就是问题所在?

最佳答案

通过在 jsonGroup 变量上运行 everyLayer 函数两次来修复它,如下所示:

function markersInRadius() {

// Lat, long of trash marker on overview map
var trashMarkerLatLng = trashMarkerOverview.getLatLng();

// counter of the amount of markers that are within a 20 meter radius
var pointsInRadius = 0;

console.log(jsonGroup);

// Loop through each point in JSON file
jsonGroup.eachLayer(function (layer) {

layer.eachLayer(function (layer) {

// Lat, long of current point
layerLatLong = layer.getLatLng();

// Distance from trashMarker
// To current point in meters
distanceFromTrashMarker = layerLatLong.distanceTo(trashMarkerLatLng);

// See if meters is within radius
if (distanceFromTrashMarker <= 20) {
pointsInRadius += 1;
};
});
});
console.log(pointsInRadius);

// Close pointsInCircle
};

关于javascript - 传单:layer.getLatLng 不适用于 .eachLayer 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31101736/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com