Enter latitude and longitude of two places, and find out the distance between them. Some handy PHP code.
Enter latitude and longitude of two places, and find out the distance between them. Click here to see the working code and to download it.
This post is tagged Tutorials

14 Comments
thanks for the code. and the redesign!
Thanks Mandy, just redesigned this thing. Will get back the comments back up too as I get the time.
FYI: This tool is useless for places that don’t fall on the same side of the prime meridian and/or equator. You should work the cardnal directions into your formula.
Thanks Zak, useful bit of info. My code above is simply a PHP version of something I came across in C. Will read up on “cardinal directions”
-Shanx.
thank you kindly for the code! i’m having a hell of a time making it work though. do you have the full page-code (php included) posted? i used the php from the text field, but somewhere between the variables being sent, and the calculation i get a “parse error” on the last line, which can’t be accurate since the last line is .
thank you,
-Confused
Hi William,
Pls send me your code through email. When PHP throws a last line error, it is usually because a semicolon is missing from somewhere in your code, or perhaps a closing bracket (the “}” character i.e.) for some loop in your code etc.
HTH,
Shanx
To pull distances from one UK postcode to another use the following SQL dump:-
http://www.good-stuff.co.uk/useful/pcodedb.sql.gz
Now, presuming you’re using MySQL, use the following converted code to have MySQL do all the math on your behalf.
SELECT
a.Town,
a.Postcode,
b.lat_dec AS lat1, b.long_dec AS long1,
c.lat_dec AS lat2, c.long_dec AS long2,
round(degrees(acos(sin(radians(b.lat_dec))
* sin(radians(c.lat_dec))
+ cos(radians(b.lat_dec))
* cos(radians(c.lat_dec))
* cos(radians(b.long_dec - c.long_dec))))*69, 2) AS distance
FROM
postcode_names a, postcode_data b, postcode_data c
WHERE
c.Postcode = ‘NR32′
AND
a.Postcode = b.Postcode
AND
round(degrees(acos(sin(radians(b.lat_dec))
* sin(radians(c.lat_dec))
+ cos(radians(b.lat_dec))
* cos(radians(c.lat_dec))
* cos(radians(b.long_dec - c.long_dec))))*69, 2)
Thats very nice but when I ran the query I got every postcode returned.
I found adding a ‘HAVING’ clause to the query did the trick - viz…
SELECT
a.Town,
a.Postcode,
b.lat_dec AS lat1, b.long_dec AS long1,
c.lat_dec AS lat2, c.long_dec AS long2,
round(degrees(acos(sin(radians(b.lat_dec))
* sin(radians(c.lat_dec))
+ cos(radians(b.lat_dec))
* cos(radians(c.lat_dec))
* cos(radians(b.long_dec - c.long_dec))))*69, 2) AS distance
FROM
postcode_names a, postcode_data b, postcode_data c
WHERE
c.Postcode = ‘NR32′
AND
a.Postcode = b.Postcode
AND
round(degrees(acos(sin(radians(b.lat_dec))
* sin(radians(c.lat_dec))
+ cos(radians(b.lat_dec))
* cos(radians(c.lat_dec))
* cos(radians(b.long_dec - c.long_dec))))*69, 2)
HAVING (distance
How do you modify above script, if I want to display distance from 1 city to another. Not all.
Having clause is missing from previous post.
I’m having same thing happening. Shows all results, and if I use limit 1 it shows first record info.
Looks like the end of the script has been missed off. Try This:-
SELECT a.Town,a.Postcode,b.lat_dec AS lat1,b.long_dec AS long1,c.lat_dec AS lat2,c.long_dec AS long2,round(degrees(acos(sin(radians(b.lat_dec))* sin(radians(c.lat_dec))+cos(radians(b.lat_dec))* cos(radians(c.lat_dec))*cos(radians(b.long_dec - c.long_dec))))*69, 2) AS distance FROM postcode_names a,postcode_data b,postcode_data c WHERE c.Postcode = ‘NR32′ AND a.Postcode = b.Postcode AND round(degrees(acos(sin(radians(b.lat_dec)) * sin(radians(c.lat_dec)) + cos(radians(b.lat_dec)) * cos(radians(c.lat_dec)) * cos(radians(b.long_dec - c.long_dec))))*69, 2) HAVING (distance
The end of the script will not show because of the less than symbol
add at the end “less than” 120)
Thanks. I found your page via Google and implemented the code in SQL (before realising someone else had done the work for me in your comments). I’m using it to calculate the distance between places I’ve taken photographs, and displaying the distances alongside a map using the new Google Maps API interface.
http://www.dankarran.com/photography/map/?town=castletown
Hi
Does anyone have a copy of the postcode sql database? Its no longer available at the URL above.
Thanks
Mark
postcode data and sql file at the attached url
http://www.easypeasy.com/guides/article.php?article=64
Incoming Links