Using only pseudo-code or JavaScript, can anyone describe the best way to determine which items in array of objects posed of:
{
"lat": float,
"lng": float
}
are within a given radius in either miles or kilometers?
I am adding geo-location-based queries to ForerunnerDB () and would like to be able to produce fast results from the search.
Bonus points if you can describe an indexing strategy that will speed up the query over the array. I have written the ForerunnerDB database from the ground up so can be flexible with integrating the answer into the code, but the main concern is query performance.
While the question pertains to a new feature of ForerunnerDB, it does not require that you go and read that project's source or familiarise yourself with that system and a pseudo-code or stand-alone JS example would be very wele!
Using only pseudo-code or JavaScript, can anyone describe the best way to determine which items in array of objects posed of:
{
"lat": float,
"lng": float
}
are within a given radius in either miles or kilometers?
I am adding geo-location-based queries to ForerunnerDB (https://github./irrelon/ForerunnerDB) and would like to be able to produce fast results from the search.
Bonus points if you can describe an indexing strategy that will speed up the query over the array. I have written the ForerunnerDB database from the ground up so can be flexible with integrating the answer into the code, but the main concern is query performance.
While the question pertains to a new feature of ForerunnerDB, it does not require that you go and read that project's source or familiarise yourself with that system and a pseudo-code or stand-alone JS example would be very wele!
Share Improve this question edited Feb 23, 2015 at 15:28 Rob Evans asked Apr 16, 2014 at 16:44 Rob EvansRob Evans 6,9985 gold badges42 silver badges57 bronze badges 2- This could be rather plicated, since geographical points are 3-dimensional. – wvdz Commented Apr 16, 2014 at 16:49
- Can't this question simply be rephrased to: how do I calculate the distance between two geographical points? – wvdz Commented Apr 16, 2014 at 16:52
2 Answers
Reset to default 4Here is a simple "direct" approach using Haversine formula:
//This function takes in latitude and longitude of two locations
// and returns the distance between them as the crow flies (in meters)
function calcCrow(coords1, coords2)
{
// var R = 6.371; // km
var R = 6371000;
var dLat = toRad(coords2.lat-coords1.lat);
var dLon = toRad(coords2.lng-coords1.lng);
var lat1 = toRad(coords1.lat);
var lat2 = toRad(coords2.lat);
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
return d;
}
// Converts numeric degrees to radians
function toRad(Value)
{
return Value * Math.PI / 180;
}
I believe this code might have e from here: Function to calculate distance between two coordinates shows wrong
The only optimisation I see is adding tangents for both latitude and longitude to cut out the results that are far outside the search region.
P.S. I really like ForerunnerDB and can't wait to see geo-related functionality
I suppose this would be answered more or less in How do I calculate distance between two latitude-longitude points?
Anyway, it might be easier to use ready made APIs such as HERE Maps API's geo-coordinate class, than creating the calculating functionality by yourself.