Calculating distances from latitude and longitude (PHP)

Enter lat­i­tude and lon­gi­tude of two places, and find out the dis­tance between them. Some handy PHP code.


Enter lat­i­tude and lon­gi­tude of two places, and find out the dis­tance between them. Click here to see the work­ing code and to down­load it.

14 comments
  1. mandy says: May 27, 20032:50 pm

    thanks for the code. and the redesign!

  2. Shashank says: Jun 01, 200312:55 am

    Thanks Mandy, just redesigned this thing. Will get back the com­ments back up too as I get the time.

  3. Zak says: Jan 12, 20041:24 pm

    FYI: This tool is use­less for places that don't fall on the same side of the prime merid­ian and/or equa­tor. You should work the card­nal direc­tions into your formula.

  4. sniptools says: Jan 13, 200412:09 am

    Thanks Zak, use­ful bit of info. My code above is sim­ply a PHP ver­sion of some­thing I came across in C. Will read up on "car­di­nal direc­tions" :)

    –Shanx.

  5. william says: Mar 23, 20047:34 am

    thank you kindly for the code! i'm hav­ing a hell of a time mak­ing it work though. do you have the full page-code (php included) posted? i used the php from the text field, but some­where between the vari­ables being sent, and the cal­cu­la­tion i get a "parse error" on the last line, which can't be accu­rate since the last line is .

    thank you,
    –Confused

  6. sniptools says: Mar 24, 20041:03 am

    Hi William,

    Pls send me your code through email. When PHP throws a last line error, it is usu­ally because a semi­colon is miss­ing from some­where in your code, or per­haps a clos­ing bracket (the "}" char­ac­ter i.e.) for some loop in your code etc.

    HTH,
    Shanx

  7. wyldie says: Apr 12, 20047:25 am

    To pull dis­tances from one UK post­code to another use the fol­low­ing SQL dump:-
    http://www.good-stuff.co.uk/useful/pcodedb.sql.gz

    Now, pre­sum­ing you're using MySQL, use the fol­low­ing con­verted 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 dis­tance
    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)

  8. Stew says: Apr 20, 20047:24 pm

    Thats very nice but when I ran the query I got every post­code 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 dis­tance
    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

  9. win says: Jul 05, 200412:57 pm

    How do you mod­ify above script, if I want to dis­play dis­tance from 1 city to another. Not all.

    Hav­ing clause is miss­ing from pre­vi­ous post.
    I'm hav­ing same thing hap­pen­ing. Shows all results, and if I use limit 1 it shows first record info.

  10. Tony Fox says: Jan 19, 20057:26 pm

    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 dis­tance 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

  11. Tony Fox says: Jan 19, 20058:57 pm

    The end of the script will not show because of the less than symbol

    add at the end "less than" 120)

  12. Dan Karran says: Jul 05, 20057:50 am

    Thanks. I found your page via Google and imple­mented the code in SQL (before real­is­ing some­one else had done the work for me in your com­ments). I'm using it to cal­cu­late the dis­tance between places I've taken pho­tographs, and dis­play­ing the dis­tances along­side a map using the new Google Maps API interface.

    http://www.dankarran.com/photography/map/?town=castletown

  13. Mark Waterhouse says: Aug 13, 20059:29 pm

    Hi
    Does any­one have a copy of the post­code sql data­base? Its no longer avail­able at the URL above.

    Thanks
    Mark

  14. nik says: Sep 29, 20051:40 am

    post­code data and sql file at the attached url

    http://www.easypeasy.com/guides/article.php?article=64

Submit comment