May 2013 Update: I’ve written a follow up post to this one, detailing how to find the distance between two locations using the Google Maps Directions API. I’ve also added the option to download the source code in full at the end of the post.
I wrote an article back in February on using the Google Maps API together with the W3C Geolocation API to show a user’s location on a map. The article was well received and I am going to build on the subject by going through how, with the help of Google Maps API once again, to find the distance between two locations; one of which could be found by using Geolocation as described in the previous article.
The method is actually quite simple; all we need to do is get the latitude and longitude co-ordinates of the two locations we want to find the distance between, and then using a mathematical formula, obtain the distance between them. These locations will depend on your application; they could be a user’s current location that is found using Geolocation, a town name, post/zipcode, or just a pre-determined set of co-ordinates – that part is up to you.