Coldfusion and Geocoding with Google & Yahoo Map API's

Recently I needed to create a Google map driven solution that required geocoded addresses. After poking around the Google Maps API and looking at other solutions I decided that I'd batch geocode the address I needed and store the coordinates in the database with the address data. This would prevent numerous geocode call to the API and should make things run a bit faster. While looking for examples of Coldfusion and Google maps I really didn't find anything that met my needs so I started from scratch. Here's what I did:

 First thing that you'll need is a Google maps key. Get one here.

<cfset ghostKey = "yourKeyGoesHere">

Next make the http call to Google. You pass in three parameters: q='address to geocode' output='xml' key='hostkey'. So your request is formatted as:

<cfset address2geocode = "4151 N. Atlantic Ave., Cocoa Beach, Fl, 32931">

<cfhttp url="http://maps.google.com/maps/geo?q=#address2geocode#&output=xml&key=#ghostKey#" resolveurl="no" />

I chose xml as output and was easy to work with. You can choose XML, KML or JSON. Look at the Google API for more info.

You can view the http results here: results.xml

Cool, two lines of code so far and we have a result. As you can see the XML is well formatted and easy to parse at this point. There are a view 'gotcha's' as well. First check to see if XML was returned.

<cfif isXML(cfhttp.filecontent)>

If so, xmlParse it:

<cfset geocodedXML = #xmlParse(cfhttp.filecontent)#>

Next you want to make sure you're getting a response code of 200, walk the XML to find Status.code:

<cfset curResponse = #geocodedXML.kml.Response.Status.code.xmlText#>

<cfif curResponse is "200">

Great! we can proceed. If you look at the result.xml file you'll see an node for Accuracy. For my application I needed an accuracy of 8.

<cfif geocodedXML.kml.Response.Placemark.AddressDetails.XmlAttributes.Accuracy eq 8><!--- if not try Yahoo to geocode --->

Now I can grab the Longitude and Latitude to update the database:

<cfset coords = #geocodedXML.kml.Response.Placemark.Point.coordinates.xmlText#>

<cfset accuracy = #geocodedXML.kml.Response.Placemark.AddressDetails.XmlAttributes.Accuracy#>

<cfset longitude = listFirst(coords)>

<cfset latitude = listGetAt(coords, 2)>

<cfquery datasource="dsn" name="updateCoords">

update myTable

set longitude = '#longitude#',

latitude = '#latitude#',

geocode_accuracy = #accuracy#

where pk = #pk#

</cfquery>

Pretty straight forward. But, if you remember I had a cfif that wanted an accuracy of 8, if not try Yahoo. No problem...just as easy as Googles...pretty much, just different. You guess it! you'll need a Yahoo API key too.

<cfelse>

<cfset yahooAppID = "yourYahooAPIKeyGoesHere">

<cfhttp url="http://local.yahooapis.com/MapsService/V1/geocode?appid=#yahooAppID#&street=#address#&city=#city#&state=#state#" resolveurl="no" />

Do the neccesary check for good reponse Yahoo's XML Result:

<cfif isXML(cfhttp.filecontent)>

<cfset geocodedXML = #xmlParse(cfhttp.filecontent)#>

Yahoo deosn't have a 'Accruacy node, they have precision and the best 'precision' you can have is address level. Any way, walk the XML grab the coords and off you go.

<cfset longitude = "#geocodedXML.ResultSet.Result.Longitude.XmlText#">

<cfset latitude = "#geocodedXML.ResultSet.Result.Latitude.XmlText#">

That's it. simple geocoding using Coldfusion and/or Yahoo! I don't blog much, but hope to get a post on how to create dynamic Google maps using Coldfusion.

 

CFDoument and Eolas patent

I finally got brave enough to publish some functionality utilizing cfdoument tags. Guess what? IE 6 and it's freaking Eolas patent patch starts breaking my delivered pdfs. I was getting a dialog box promting my to 'open''save''canel' the *.cfm file that was generating the pdf. You could save the pdf...I mean .cfm file then change it to a .pdf, poof file opens as expected.

The CFMX 7.0.2 updater fixes this issue. It's not documented well if this is actually what fixed my issue...anyway, it fixed it.

My dev environment was 7.0.2 so I didn't notice issues until it was published to prod. I guess I just felt like ranting a bit. I still love CF! Microsoft....not so much.

What's a blog?

I think it's like having a pet. You have to feed it (with entries and comments) or it will die. You have to protect it (deleted spam trackbacks) or it will die. If you go on vacation you have to update it about your vacation, or it will die.

My blog has never chewed my slippers, soiled my carpet or aggravated my allergies. Good blog.

Will my blog make it? doubtful.

Certified

I been trying to get around to taking this exam for three years now. Since then I had to migrate from Coldfusion 5.5, to ver 6, then to ver 7. This would have been lots easier if I had taken it three years ago as the functionality of CF has continued to grow. Anyway, I took the exam at Adobe MAX conference in Las Vegas and I got an 86%....Yahoooo!!! 1 % higher than I need to get 'Advanced' developer status. What does this mean now? Now that I'm all certified and everything? I dunno. So, three cheers for me!

Dallas Twiford | email:dallasweb at yahoo dot com | Contact MissionDesign