Find distance between two locations

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

Find distance between two locations

Post 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"..
rmouali
Forum Newbie
Posts: 3
Joined: Sat May 12, 2007 5:51 pm

Post 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
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

It may be an idea in future to search these forums before you post. This has been discussed frequently...
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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. :-)
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

Post by GeXus »

Very interesting... .thanks!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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
Post Reply