Page 1 of 1

Find distance between two locations

Posted: Sun May 13, 2007 1:46 pm
by GeXus
Anyone know what data or service lets you determine the distance between two places, such as "There are 5 restaurants within a 25 mile radius"..

Posted: Sun May 13, 2007 2:41 pm
by rmouali
GoogleMapAPI do that. It's a PHP class that implement Google MAP Api.
geoGetDistance($lat1,$lon1,$lat2,$lon2,$unit)
---------------------------------------------

This gets the distance between too coorinate points using the great
circle formula. $unit can be M (miles),K (kilometers),N (nautical
miles),I (inches), or F (feet). Default is M.

Example:

$distance = $map->geoGetDistance($lat1,$lon1,$lat2,$lon2,$unit);
The API is available in this adress

Posted: Sun May 13, 2007 2:42 pm
by patrikG
Use the Haversine formula:

http://en.wikipedia.org/wiki/Haversine_formula

Implementation (although in Javascript):

http://www.movable-type.co.uk/scripts/latlong.html

Posted: Sun May 13, 2007 4:48 pm
by CoderGoblin
It may be an idea in future to search these forums before you post. This has been discussed frequently...

Posted: Sun May 13, 2007 8:54 pm
by Ambush Commander
Nah, I think it's a valid question. Even though the answer's been iterated many times over, parent post doesn't know specific enough terminology to look it up (hey, I didn't know what haversines are: learned something new!) In this case, the most useful answer is a pointer in the right direction, and not necessarily a solution for the problem. :-)

Posted: Mon May 14, 2007 10:33 am
by GeXus
Very interesting... .thanks!

Posted: Mon May 14, 2007 11:07 am
by RobertGonzalez
The typical term for this is 'proximity' searching. There are a fairly large number of tools available for this, some implementing the math completely on the database server, some doing half in the database and half on the web server.

It is a fairly expensive process, in terms of what you put both servers through, so it would always be a good idea to properly index and optimize your data. When doing this.

PS In addition to Haversine, you might also want to look at something called the 'Great Circle Radius' as this plays into accuracy when traversing a direction atop a sphere.

Posted: Tue Jun 19, 2007 6:53 am
by patrikG
A SQL-implementation is to be found at http://blog.peoplesdns.com/archives/24
However, the distance can be faulty, use this type of query instead
(latitude: 33.887306, longitude: -117.894628 - replace as necessary)

Code: Select all

SELECT 3956 * 2 * atan2(sqrt(pow(sin(((latitude * 3.14159265359 /180.0)-(33.887306 * 3.14159265359 /180.0))/2.0),2) + cos((33.887306 * 3.14159265359 /180.0)) * cos((latitude * 3.14159265359 /180.0)) * pow(sin(((longitude * 3.14159265359 /180.0)-(-117.894628 * 3.14159265359 /180.0))/2.0),2)),sqrt(1-(pow(sin(((latitude * 3.14159265359 /180.0)-(33.887306 * 3.14159265359 /180.0))/2.0),2) + cos((33.887306 * 3.14159265359 /180.0)) * cos((latitude * 3.14159265359 /180.0)) * pow(sin(((longitude * 3.14159265359 /180.0)-(-117.894628 * 3.14159265359 /180.0))/2.0),2))))) as distance FROM TABLE_NAME