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

Find distance between two locations

Postby GeXus » Sun May 13, 2007 1:46 pm

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"..
GeXus
Forum Regular
 
Posts: 628
Joined: Sat Mar 11, 2006 9:59 am

Postby rmouali » Sun May 13, 2007 2:41 pm

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

Postby patrikG » Sun May 13, 2007 2:42 pm

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

Postby CoderGoblin » Sun May 13, 2007 4:48 pm

It may be an idea in future to search these forums before you post. This has been discussed frequently...
User avatar
CoderGoblin
DevNet Resident
 
Posts: 1425
Joined: Tue Mar 16, 2004 11:03 am
Location: Aachen, Germany

Postby Ambush Commander » Sun May 13, 2007 8:54 pm

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. :-)
HTML Purifier for Standards-Compliant HTML Filtering
User avatar
Ambush Commander
DevNet Master
 
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Postby GeXus » Mon May 14, 2007 10:33 am

Very interesting... .thanks!
GeXus
Forum Regular
 
Posts: 628
Joined: Sat Mar 11, 2006 9:59 am

Postby RobertGonzalez » Mon May 14, 2007 11:07 am

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.
PHPDN's Most Friendly 2006, Most Helpful, Friendliest 2007, Friendliest, Peacemaker 2008 | Zend Certified Engineer, Zend Framework Certified Engineer
Common Forum Answers | PHPDN Forum Tour | <?php $me = `whoami`; ?> | <?php while (!$succeed) $try++; ?>
I don't offer solutions via PM. I's much more helpful to the community to solve problems in the open so others can benefit from the solution as well.
Help keep our boards online and ad-free.
to support our forums.
User avatar
RobertGonzalez
Site Administrator
 
Posts: 14254
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Postby patrikG » Tue Jun 19, 2007 6:53 am

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)

Syntax: [ Download ] [ Hide ]
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
User avatar
patrikG
DevNet Master
 
Posts: 4238
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 1 guest