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.

  • mandy

    thanks for the code. and the redesign!

  • Shashank

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

  • Zak

    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.

  • / snip­tools

    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 directions" :)

    –Shanx.

  • william

    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

  • / snip­tools

    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

  • wyldie

    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)

  • Stew

    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

  • win

    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.

  • Tony Fox

    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

  • Tony Fox

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

    add at the end "less than" 120)

  • Dan Kar­ran

    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

  • Mark Water­house

    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

  • http://www.easypeasy.com/guides/article.php&article=64 nik

    post­code data and sql file at the attached url

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