通过Google Maps API进行地理编码随机失败

时间:2018-04-12 14:39:37

标签: google-maps google-maps-api-3 coldfusion sql-server-2014 coldfusion-10

我有一个包含3种不同类型地址的表单(物理,邮件和商业)。当我提交表单时,我使用下面的代码来获取地址的纬度和经度。

但是,我得到了零星的结果。有时候他们都会工作,有时候只有1件工作,等等。我正在寻找关于为什么会这样做的想法。

<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>

1 个答案:

答案 0 :(得分:2)

您未在请求中包含(现在必需的)key参数。

(顺便说一句,你要包含sensor参数,这是不再需要的)