I have a form containing 3 different types of addresses on it (Physical, Mailing and Business). When I submit the form I use the code below to get the latitude and longitude for the address(es).
However, I'm getting sporadic results. Sometimes they all work and other times only 1 works, etc. I'm looking for ideas as to why it is acting like this.
<cfif form.f_address IS NOT "" AND form.f_city IS NOT "" AND form.f_state IS NOT "" AND form.f_zip IS NOT "">
<CFQUERY datasource="#datasource#" name="get_mstate">
select state_abb from states
where state_id = '#form.f_state#'
</CFQUERY>
<cfoutput>
<cfset maddress2geocode = "#Form.f_address#, #form.f_city#, #get_mstate.state_abb# #form.f_zip#">
<cfset mapi = "http://maps.googleapis.com/maps/api/geocode/json?address=#URLEncodedFormat(maddress2geocode)#&sensor=false">
<cfhttp url="#mapi#"></cfhttp>
<cfset mresponse = DeserializeJSON(cfhttp.FileContent)>
<cftry>
<cfset mpoint = mresponse["results"][1]["geometry"]["location"]>
#SerializeJSON(mpoint)#
<cfquery name="insert_mgeocode" datasource="#datasource#">
UPDATE contactrecord
SET lat = <cfqueryparam value="#mpoint.lat#">,
long = <cfqueryparam value="#mpoint.lng#">
WHERE contactrecord_id = '#newid#'
</cfquery>
<cfcatch>
<cfquery name="insert_mfailed" datasource="#datasource#">
UPDATE contactrecord
SET failed_geocode = '1'
WHERE contactrecord_id = '#newid#'
</cfquery>
</cfcatch>
</cftry>
</cfoutput>
</cfif>
<cfif form.f_paddress IS NOT "" AND form.f_pcity IS NOT "" AND form.f_pstate IS NOT "" AND form.f_pzip IS NOT "">
<CFQUERY datasource="#datasource#" name="get_pstate">
select state_abb from states
where state_id = '#form.f_pstate#'
</CFQUERY>
<cfoutput>
<cfset paddress2geocode = "#Form.f_paddress#, #form.f_pcity#, #get_pstate.state_abb# #form.f_pzip#">
<cfset papi = "http://maps.googleapis.com/maps/api/geocode/json?address=#URLEncodedFormat(paddress2geocode)#&sensor=false">
<cfhttp url="#papi#"></cfhttp>
<cfset presponse = DeserializeJSON(cfhttp.FileContent)>
<cftry>
<cfset ppoint = presponse["results"][1]["geometry"]["location"]>
#SerializeJSON(ppoint)#
<cfquery name="insert_pgeocode" datasource="#datasource#">
UPDATE contactrecord
SET phy_lat = <cfqueryparam value="#ppoint.lat#">,
phy_long = <cfqueryparam value="#ppoint.lng#">
WHERE contactrecord_id = '#newid#'
</cfquery>
<cfcatch>
<cfquery name="insert_pfailed" datasource="#datasource#">
UPDATE contactrecord
SET failed_geocode = '1'
WHERE contactrecord_id = '#newid#'
</cfquery>
</cfcatch>
</cftry>
</cfoutput>
</cfif>
<cfif form.f_baddress IS NOT "" AND form.f_bcity IS NOT "" AND form.f_bstate IS NOT "" AND form.f_bzip IS NOT "">
<CFQUERY datasource="#datasource#" name="get_bstate">
select state_abb from states
where state_id = '#form.f_bstate#'
</CFQUERY>
<cfoutput>
<cfset baddress2geocode = "#Form.f_baddress#, #form.f_bcity#, #get_bstate.state_abb# #form.f_bzip#">
<cfset bapi = "http://maps.googleapis.com/maps/api/geocode/json?address=#URLEncodedFormat(baddress2geocode)#&sensor=false">
<cfhttp url="#bapi#"></cfhttp>
<cfset bresponse = DeserializeJSON(cfhttp.FileContent)>
<cftry>
<cfset bpoint = bresponse["results"][1]["geometry"]["location"]>
#SerializeJSON(bpoint)#
<cfquery name="insert_bgeocode" datasource="#datasource#">
UPDATE contactrecord
SET bus_lat = <cfqueryparam value="#bpoint.lat#">,
bus_long = <cfqueryparam value="#bpoint.lng#">
WHERE contactrecord_id = '#newid#'
</cfquery>
<cfcatch>
<cfquery name="insert_bfailed" datasource="#datasource#">
UPDATE contactrecord
SET failed_geocode = '1'
WHERE contactrecord_id = '#newid#'
</cfquery>
</cfcatch>
</cftry>
</cfoutput>
</cfif>
You are not including the (now mandatory) key
parameter in the request.
(as an aside, you are including the sensor
parameter, which is no longer required)