Leaflet formula: Counting markers within a radius (version 2)
A couple of years ago, I open-sourced a project that allows you to count how many markers are within a circle using Leaflet. As part of the template, a user needs to enter an address to get a marker to show up on the map.
Two weeks ago, Jefferson Guerrón reached out to me because he was creating something similar that didn’t require a user to type in their address. He was using the plugin Leaflet.draw to get the marker on the map. But he was having a hard time getting a circle to show up around the marker and count the other markers within that circle.
After a few back-and-forth emails, we were able to come up with a solution. Here’s a look of the final project. And the code is available on Github.
If you have questions, don’t hesitate to e-mail or comment.
Hello,
I would like to use part of the code on a project Im working on.
Unfortunately Im really new to Json. Where would I use my JS to get the markers within the radius to be listed in a textbox next to the map?
Would really really appreciate the help.
Thanks
Werner Swart
May 17, 2017 at 2:07 am
Hi Werner,
Do you have a project started? And if so, what do you have so far?
csessig
May 17, 2017 at 3:06 pm
This is cool but there are some mistakes. For example, the distance on the sidebar is showing meters but it says miles. So you should correct it or use: (distance_from_layer_circle * 0.000621371).toFixed(2) so it reads correctly. I am also not sure that the actual radius that the circle spans is accurate. I am selecting 500 miles from the list but even with locations at the outer edge of the circle, there is no way they are even close to 500 miles from the draw marker latlng. This can be confirmed when the actual distances show up in miles, the most I am getting is ~50 miles when I have 500 selected. Thank you.
Greg
December 13, 2017 at 3:11 pm
Thanks for catching those things Greg. They should be resolved now.
csessig
December 14, 2017 at 5:34 pm