SniptoolsSniptools | Design & Technology Observations

RSS

Calculating distances from latitude and longitude (PHP)

May 27th 2003
14 Comments

Respond
Trackback

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

14 Comments

  1. mandy

    thanks for the code. and the redesign!

  2. Shashank

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

  3. Zak

    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.

  4. Shashank Tripathi

    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.

  5. william

    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

  6. Shashank

    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

  7. wyldie

    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)

  8. Stew

    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

  9. win

    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.

  10. 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 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

  11. Tony Fox

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

    add at the end “less than” 120)

  12. Dan Karran

    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

  13. Mark Waterhouse

    Hi
    Does anyone have a copy of the postcode sql database? Its no longer available at the URL above.

    Thanks Mark

  14. postcode data and sql file at the attached url http://www.easypeasy.com/guides/article.php?article=64

Incoming Links