传单:layer.getLatLng无法使用.echLayer函数

Leaflet: layer.getLatLng not working with .eachLayer function

本文关键字:echLayer 函数 layer getLatLng 传单      更新时间:2023-09-26

我正在尝试做一些与Chris Essig在这里所做的非常相似的事情。因为我需要知道在用户放下"垃圾标记"的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 FeatureGroup上做了console.log之后,我发现该组的层选项卡中有两个对象,没有任何平台信息,而是有一个自己的层选项卡,包含所有具有锁定信息。。。也许这就是问题所在?

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

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