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.

 

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