Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leaflet markers within a radius #101

Open
jagb1007 opened this issue Nov 24, 2016 · 1 comment
Open

Leaflet markers within a radius #101

jagb1007 opened this issue Nov 24, 2016 · 1 comment
Labels

Comments

@jagb1007
Copy link

Good morning can help me with some tutorial, where I can draw a marker anywhere on the map and by a combo box can choose a distance and I can draw a circle by seeing the names of markers that are in that circle, something like that

draw radius

@sfairgrieve
Copy link
Contributor

This seems like a more general Leaflet JS question, but I can try to answer it. You can use Leaflet.Draw for the drawing functionality. When a user draws a circle, you can get the radius in meters of that circle. Or, you can let the user place a marker using Leaflet.draw and then add a new L.Circle layer to the map using the radius from the combobox. Once you have center point + radius, you can use TurfJS to help with the computation of finding points within the polygon/circle. turf.circle will create a circle polygon given a center point and radius, and then you can use turf.within to find the points that fall within that circle polygon. This assumes you're using GeoJSON for representing points. If you're not using GeoJSON, you could do something more basic like calculating the distance of every point from the center point and then only keeping points that fall within the user desired radius. Leaflet JS includes functions for making these calculations while taking into account the curvature of the Earth. Turf JS also includes the distance function, which performs a similar calculation.

Are you storing/serving up these points through a web service, or will all points be added to the map then filtered? The above approaches will work fine with 100s/1000s of points but may be slow/lock up the browser with larger numbers of points. If you have a lot of points that you're trying to filter, you should probably stick those in a geospatial database or other geo services that move those calculations out of the browser.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants