DEVseo

Making It Your Web

Get The Distance Between Two Addresses

Find The Distance Between Two Addresses

After having a lot of fun working on the "Find Your Nearest ATM" script I decided to have a go at something similar, but a little bit different.

Some Background

This is actually going to be part of a project I'm currently working on in my job at Worldview Limited. I have recently been writing a "Proposal Generator", which uses a lovely JavaScript "Web App" interface to add options and text to generate a PDF "proposal" for our corporate clients.

Part of the specification for the new system was to include the distance from a given location to one of our supplier locations. This needed to be dynamic based on the two addresses given so I decided to have a hunt around and see what I could find.

So, what did I do?

The first (and best) resource I found was at Brian Cray's Blog which had the exact thing that I need, except for one exception: it was written a couple of years ago and used the old Google Maps API. So, the challenge was to convert it to use v3 of the Google Maps API and jQuery.

The other function of the tool would be a requirement to choose whether to show the distance "as the crow flies" or the driving distance. Now Brian has also written another blog detailing how to get the distance between two given addresses (which can be found here) but I found a much easier way than that on StackOverflow that uses the "Haversine formula", requiring no look up to Google Maps but is still very accurate. So, I decided to use a mix and a form to get everything and even convert the distance between miles and kilometres. Click below to give it a go:

Find The Distance Between Two Addresses

The only improvement I can see it needing is the ability to select an address from a list when more than one result is returned from the API (all address lookups use the Google Maps API to return a lat and long position). At the moment it simply uses the first (and closest) match to the given address. Because of this I recommend being as precise as possible, but it does work for most positions across the globe. The only ones I couldn't get results were from Europe to America, but then Europe to Australia did return me a result!

Give it a go and let me know what you think in the comments here.

Comments (be the first)

Add A Comment

Please note: all comments are moderated before they are published on this page.


09igtd
Cancel Reply

Loading

Complete!