简体   繁体   English

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

[英]Geocoding via Google Maps API is failing randomly

I have a form containing 3 different types of addresses on it (Physical, Mailing and Business). 我的表格上包含3种不同类型的地址(物理地址,邮件地址和商业地址)。 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. 有时它们都可以工作,有时只有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>

You are not including the (now mandatory) key parameter in the request. 您没有在请求中包含(现在是必需的) key参数。

(as an aside, you are including the sensor parameter, which is no longer required) (顺便说一句,您将包括不再需要的sensor参数)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM