Find distance between two locations
Moderator: General Moderators
Find distance between two locations
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"..
GoogleMapAPI do that. It's a PHP class that implement Google MAP Api.
The API is available in this adressgeoGetDistance($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);
Use the Haversine formula:
http://en.wikipedia.org/wiki/Haversine_formula
Implementation (although in Javascript):
http://www.movable-type.co.uk/scripts/latlong.html
http://en.wikipedia.org/wiki/Haversine_formula
Implementation (although in Javascript):
http://www.movable-type.co.uk/scripts/latlong.html
- CoderGoblin
- DevNet Resident
- Posts: 1425
- Joined: Tue Mar 16, 2004 10:03 am
- Location: Aachen, Germany
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
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. 
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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.
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.
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)
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