Jump to content

Calculating Lat/Long distances with PHP

Recommended Posts

I'm trying to write myself a webpage for a travel bug that shows distance to a target cache.

So, due to extreme laziness I wondered if anyone has a script for calculating the distance between 2 lat/long coordinate that they don't mind sharing to save me trying myself.


Thanks in advance for any help!

Link to comment

I have googled around before for a library, I can't remember exactly where I got it from, but I have the source on my site.

Library Example usage


There may also be a pear lib, but I can't remember.


Edit: I forgot to add, LordElph's opensourced geograph project has php classes too. It used to be on sourceforge, but they moved the project off there. (I'll add his JS version is also excellent, and underpins my OS mapping script)

Edited by Edgemaster
Link to comment

i've just been having a look at this and am now confused.


I've added scripts suggested and also one that i've converted from Java on THIS SITE to my site.


My page is http://www.bambography.co.uk/geocaching/distance.php


When I use the same coords on my page and on the movable type page I get different distances. I have to admit that I don't fully understand this so there is probably something i'm missing. Does anyone know what, or am I doing something wrong?

Cheers in advance!


Link to comment

Thats the one i've been trying to convert. When I add the coords to that page it gives a difference answer to my php version of the same script...?!?


Tried your script in NuSphere PhpED, and got the same answer as the other site. This suggests that your function is not getting the values you think it is getting.

Link to comment

I listed a few classes earlier, I've since restructured my site, fixed urls are: classes and usage example. Knock an s off the extension to see the actual pages.


Disclaimer: I've not done much accuracy testing for this, I didn't write it and am not using it properly yet.


Edit: I've also added LordElph's geograph conversion to the example page, to compare the results. It would seem that 18km is a good result for these coords.

Edited by Edgemaster
Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...