Reputation: 62704
I have a list with 50 items:
var mylocations = [{'id':'A', 'loc':[-21,2]},...,];
How can I in leaflet or JavaScript most efficiently make it such that if I accept an input of a specific location [<longitude>,<latitude>]
, a radius (e.g. 50 miles)... I can get all of the "mylocations"
that fall within that circle?
Using external libraries is fine.
Upvotes: 3
Views: 4685
Reputation: 28688
Leaflet's L.LatLng
objects include a distanceTo
method:
Returns the distance (in meters) to the given LatLng calculated using the Haversine formula.
http://leafletjs.com/reference.html#latlng-distanceto
var inRange = [], latlng_a = new L.LatLng(0, 0), latlng_b;
locations.forEach(function (location) {
latlng_b_ = new L.LatLng(location.pos[0], location.pos[1]);
if (latlng_a.distanceTo(latlng_b) < 80.4672) {
inRange.push(location);
}
});
Upvotes: 10