Wednesday, September 27, 2006

Replacement for 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
& this.key & "' type='text/javascript'></script>"
& chr(13) & chr(10) />
<cfreturn jsstring />

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 = "">
<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 = ListGetAt(Trim(cfhttp.fileContent),3) />
<cfset geocode.lon = ListGetAt(Trim(cfhttp.fileContent),4) />
<cfset = 0 />
<cfset geocode.lon = 0 />

<cfreturn geocode />

No comments: