Chris Essig

Walkthroughs, tips and tricks from a data journalist in eastern Iowa

Leaflet formula: Counting markers within a radius (version 2)

with 4 comments

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.

Advertisements

Written by csessig

December 14, 2016 at 5:15 pm

4 Responses

Subscribe to comments with RSS.

  1. 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

  2. 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


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: