Wednesday, September 27, 2006

Replacement for geocoder.us functionality in cfgooglemaps

I was playing with the cfgooglemaps CFC. It's a great CFC, but I was having a few small problems.

Since most of the addresses I needed to map were non-American, it was returning blank locations, and the non-commercial license bothered me (not that I'm doing anything commercial at this point).

So I rewrote it using the new Geocoding features in the Google Maps API.

There were two easy steps:

1. Replace the 'v' parameter in the main JavaScript include line to be version 2 instead of 1.

<cffunction name="getMapJS" access="public" returntype="string"
description="Get javascript include">
<cfset jsstring = "<script
src='http://maps.google.com/maps?file=api&v=2&key="
& this.key & "' type='text/javascript'></script>"
& chr(13) & chr(10) />
<cfreturn jsstring />
</cffunction>


2. Replace the geocode_address function as indicated below.

<cffunction name="geocode_address" access="private"
returntype="struct" description="Geocode address using
Google geocoding">
<cfargument name="address" required="true" type="string" />

<cfset newUrl = "http://maps.google.com/maps/geo">
<cfset newUrl = newUrl & "?q=#URLEncodedFormat(address)#">
<cfset newUrl = newUrl & "&key=#this.key#&output=csv">

<cfhttp method="get" url="#newUrl#" charset="UTF-8"></cfhttp>

<cfset geocode = StructNew() />

<cfif ListLen(Trim(cfhttp.fileContent)) EQ 4>
<cfset geocode.lat = ListGetAt(Trim(cfhttp.fileContent),3) />
<cfset geocode.lon = ListGetAt(Trim(cfhttp.fileContent),4) />
<cfelse>
<cfset geocode.lat = 0 />
<cfset geocode.lon = 0 />
</cfif>

<cfreturn geocode />
</cffunction>

No comments: