All pastes #707787 Raw Edit

Stuff

public text v1 · immutable
#707787 ·published 2007-09-23 08:58 UTC
rendered paste body
<cfcomponent>
<cffunction name="Geocode" access="remote" returntype="struct" hint="sends data to google, and returns lat/long">
  <cfargument name="fullAddress"	type="string"	required="false" default=""  hint="full address string bypassing the arguments">
  <cfargument name="address1"	    type="string"	required="false" default=""  hint="street address line 1">
  <cfargument name="address2"	    type="string"	required="false" default=""  hint="street address line 2">
  <cfargument name="city"			type="string"	required="false" default=""  hint="city">
  <cfargument name="state"		type="string"	required="false" default=""  hint="state">
  <cfargument name="postalCode"	type="string"	required="false" default=""  hint="postal code">
  <cfargument name="countryCode"	type="string"	required="false" default=""  hint="the country code that we use to look up the states for">
  <cfargument name="googleMapKey" type="string"	required="false" default=""  hint="your google map key for your site">
  <cfargument name="attempt"		type="numeric"	required="false" default="1" hint="number of times we've tried to find a code">
  <!--- Declare local variables --->
  <cfset var instance = structNew()>
  <cfset var cfhttp	= structNew()>
  <!--- Logic --->
  <!--- Create address string based on what attempt this is --->
  <cfif len(arguments.fullAddress)>
    <cfset instance.addressString = arguments.fulladdress>
    <cfelse>
    <cfif arguments.attempt IS 1>
      <cfset instance.addressString = "#arguments.address1# #arguments.address2#, #arguments.city#, #arguments.state# #arguments.postalCode# #arguments.countryCode#">
      <cfelseif arguments.attempt IS 2>
      <cfset instance.addressString = "#arguments.address1#, #arguments.City#, #arguments.State# #arguments.postalCode# #arguments.countryCode#">
      <cfelseif arguments.attempt IS 3>
      <cfset instance.addressString = "#arguments.address1#, #arguments.City#, #arguments.State# #arguments.countryCode#">
      <cfelseif arguments.attempt IS 4>
      <cfset instance.addressString = "#arguments.City#, #arguments.State# #arguments.countryCode#">
      <cfelseif arguments.attempt IS 5>
      <cfset instance.addressString = "#arguments.City#, #arguments.countryCode#">
      <cfelse>
      <cfset instance.addressString = "#arguments.countryCode#">
    </cfif>
  </cfif>
  <!---<cfdump var="#instance#">
			<cfdump var="#arguments#">--->
  <!--- replace all spaces with + sign because thats the way google geocoder likes it --->
  <cfset instance.addressString = replace(instance.addressString," ","+","all")>
  <!--- send geocoder request to google --->
  <cfhttp method="get" url="http://maps.google.com/maps/geo?q=#instance.addressString#&output=xml&key=#arguments.googleMapKey#" resolveurl="no" ></cfhttp>
  <!---<cfoutput>#htmleditFormat(cfhttp.filecontent)#</cfoutput>--->
  <cftry>
    <!--- parse the xml file that is returned --->
    <cfset instance.xmlFile = xmlparse(cfhttp.filecontent)>
    <!--- extract the lat long coordinates out of the xml file --->
    <cfset instance.locationList = instance.xmlFile.kml.response.placemark.point.coordinates.XmlText>
    <cfcatch type="any">
      <!--- recall the geocode function with less accurate coordinates --->
      <cfset arguments.attempt = arguments.attempt + 1>
      <cfif NOT len(arguments.fullAddress)>
        <cfset instance.sReturn  = Geocode(argumentCollection=arguments)>
      </cfif>
    </cfcatch>
  </cftry>
  <cfset instance.sReturn = structNew()>
  <cfset instance.sReturn.hasGeocode = false>
  <cfif isDefined("instance.locationList")>
    <cfif listLen(instance.locationList) GTE 2>
      <cfset instance.sReturn.hasGeocode	= true>
      <cfset instance.sReturn.Lon			= listGetAt(instance.locationList,1)>
      <cfset instance.sReturn.Lat			= listGetAt(instance.locationList,2)>
      <cfset instance.sReturn.Accuracy	= arguments.attempt>
    </cfif>
  </cfif>
  <!--- return --->
  <cfreturn instance.sReturn/>
</cffunction>
</cfcomponent>