0

I'm using the leaflet draw tool to create a circle and then finding the points inside the circle. My data is from GeoJSON, and the code works as far as figuring out how many points are inside, but I want to then capture the point in a new array, test.push.layer doesn't work, it seems to create a null object, not a layer.

if (type == 'circle') {
    var test = [];

    var theCenterPt = layer.getLatLng();
    var theRadius = layer.getRadius();
    var counter_points_in_circle = 0;

    // Loop through each point in GeoJSON file  //var allPoints = L.geoJson(data); 
    allPoints.eachLayer(function (layer) {
        var theLayer = layer;
        // Lat, long of current point
        layer_lat_long = layer.getLatLng();
        // Distance from our circle marker To current point in meters
        distance_from_centerPoint = layer_lat_long.distanceTo(theCenterPt);
        // See if meters is within radius
        if (distance_from_centerPoint <= theRadius) {
            counter_points_in_circle += 1;

            test.push.layer;     //Fails

        }

    });
    alert(counter_points_in_circle);  //Works.
    alert(test.features.length);    //test.features is undefined..
    alert("results "+JSON.stringify(test));  //results [] 
    alert("results "+JSON.stringify(test.toGeoJSON()));  //test.toGeoJSON is not a function
}
0

Your test variable is declared as an array. You should use:

test.push(layer)

test.length

L.layerGroup(test).toGeoJSON()

0

This is what finally worked..

test.push(layer.feature);

Then I could see my results in

alert(test.length);   
alert("results "+JSON.stringify(test));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.