Using ColdFusion to determine the distance between two addresses

I’m not sure this is extremely accurate, or useful, but I’ve wanted to write up a quick demo on this for a while, so today was the day. The question is simple. Given two addresses, is it possible to find the distance between them? Since the answer to “Can you do X in ColdFusion” is always yes (warning, I may be a bit biased), here is the code I used to demonstrate this. First off, we need to make use of two resources. The first is the ColdFusion Yahoo Package, a collection of CFC wrappers for Yahoo web services. One of them in particular: geoCoding. The geoCoding service lets you pass in an address (or even just a zip) and it will return geographical information. The important bits are longitude and latitude.

Once we have that, we can then calculate an estimated distance between the two long/lat values. I used LatLonDist from CFLib. There are other ways to do this and I’ll specifically call out Ben Nadel’s example as an alternative.

Ok, so once we have that, how can we combine the two tools? Consider the following demo:

<cfset geoAPI = createObject("component", "")>

<cfset address1 = “403 Robinhood Circle, Lafayette, LA 70508”> <cfset address2 = “1835 73rd Ave NE, Medina, WA 98039”>

<cfset geodetails1 = geoAPI.geoSearch(location=address1)> <cfset geodetails2 = geoAPI.geoSearch(location=address2)>

<cfset distance = latLonDist(geodetails1.latitude, geodetails1.longitude, geodetails2.latitude, geodetails2.longitude,’sm’)>

<cfoutput>Distance between #address1# and #address2# is #numberFormat(distance,”9.9”)# miles.</cfoutput> </code>

First off, let me be sure folks note that the UDF is not included in the code block above. I wanted to minimize the example as much as possible, so if you wanted to run this code as is, you would need to copy the source from CFLib first. You would also need to ensure the CFYahoo package was downloaded and available via the org mapping. Outside of that though the code is trivial. I’ve got my two addresses, both of which are passed to the geocoding CFC. This returns a structure that includes the longitude and latitude. Once I have them, I pass them to the UDF and ask for the result in statute miles.

The demo addresses give me a result of 2000.1 miles. That’s a bit too precise so I did a quick test on Google Maps. Using driving directions, it gave me a result of 2907 miles. I noticed the path though wasn’t exactly a straight line:

Not sure if this is useful or not, but enjoy.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support.

Lafayette, LA