传单:layer.getLatLng 不使用 .eachLayer 函数

Leaflet: layer.getLatLng not working with .eachLayer function

我正在尝试做一些与 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 FeatureGroup 执行 console.log 之后,我发现该组的图层选项卡中有两个对象,没有任何经纬度信息,而是它自己的图层选项卡,包含所有数据点 latlng 信息...也许这就是问题所在?

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

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