Using ColdFusion to determine the distance between two addresses

This post is more than 2 years old.

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", "org.camden.yahoo.geocoding")>

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

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 senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by Scott P posted on 5/25/2009 at 8:40 PM

but he seemed like a normal guy, we didn't see this coming your honor.

Comment 2 by Raymond Camden posted on 5/25/2009 at 8:41 PM

Eh? :)

Comment 3 by James Edmunds posted on 5/25/2009 at 8:44 PM

Excellent! This is actually very useful, and I may look at it as a replacement for a zipcode table driven distance lookup I have on a web site. However, I'm always a bit leery of third-party services it terms of availability, so I would likely look for an alternative process for the user in the case of a timeout... Does the component package on RIAForge have some kind of settable time out error return?

Comment 4 by Raymond Camden posted on 5/25/2009 at 8:47 PM

No, you would have to manually add a timeout value to the cfhttp tag in the CFC. The CFC only has one method though so it would be quick to add. You would then need to wrap the calls w/ try/catch of course.

Comment 5 by James Edmunds posted on 5/25/2009 at 8:57 PM

Cool. I could just send them to my old zip-lookup local version should the web service stall. Come to think of it, I would also revert back to that in the case when my database of addresses serves up a PO Box. In any case, I love this demo utilizing material from two of my favorite resources, RIAForge and CFLib!

Comment 6 by Raymond Camden posted on 5/25/2009 at 9:43 PM

I think I may do a quick blog post on handling errors with remote APIs. Just some basic examples. For example, how to handle trivial breakage (getting the high temp for the day), not-so-trivial (getting shipping costs), and critical (remote ecom process that must work).

Comment 7 by JC posted on 5/26/2009 at 9:08 PM

Here's a much less useful way to determine distance in CF... as the propeller beanie flies instead of the car drives...

http://yougiveloveabad.name...