parker
parker

Reputation: 9

ColdFusion XML Parsing Multiple Responses

I am using the Google Maps API and am trying to geocode a certain address. However, for some reason Google is returning similar addresses from the entire country, despite me specifying a city, state and zip. So now I have an XML response that is huge. If you run the code below in your own CF template you will see what I am talking about.

I am trying to get the geocode for the "Bakersfield CA" address. However, since Google returns the "Homer, LA" response first, that is what my code is picking up.

<cfhttp url="http://maps.googleapis.com/maps/api/geocode/xml?address=327%20BEARDSLEY%20AVE%20&city=Bakersfield&state=CA&zip=93308" method="get" timeout="300">                       </cfhttp>      

<cfif Left(CFHTTP.StatusCode, 3) EQ "200">                                  
    <cfset xmlResponse = XmlParse(CFHTTP.FileContent)>
    <cfset mylat = xmlResponse.GeocodeResponse.result.geometry.location.lat.xmlText>
    <cfset mylon = xmlResponse.GeocodeResponse.result.geometry.location.lng.xmlText>

    #mylat#                 
</cfif>


FOR DEBUGGING 
    <cfoutput>
    <cfdump var="#xmlResponse#">
    </cfoutput>

Upvotes: 0

Views: 187

Answers (1)

geocodezip
geocodezip

Reputation: 161334

The Google Maps Geocoding webservice does not support the parameters you are giving it (city, state, zip), so it sees:

327 BEARDSLEY AVE

Which is far from completely specified.

If you put the complete address the correct parameter (address) it will give you the result you expect:

http://maps.googleapis.com/maps/api/geocode/xml?address=327%20BEARDSLEY%20AVE,%20Bakersfield,%20CA,%2093308

Parameters supported (per the documentation referenced above)

Geocoding (Latitude/Longitude Lookup)

Required parameters in a geocoding request:

address — The address that you want to geocode. or components — A component filter for which you wish to obtain a geocode. See Component Filtering for more information. The components filter will also be accepted as an optional parameter if an address is provided.

Optional parameters in a geocoding request:

bounds — The bounding box of the viewport within which to bias geocode results more prominently. This parameter will only influence, not fully restrict, results from the geocoder. (For more information see Viewport Biasing below.)

key — Your application's API key. This key identifies your application for purposes of quota management. Learn how to get a key from the APIs Console.

language — The language in which to return results. See the list of supported domain languages. Note that we often update supported languages so this list may not be exhaustive. If language is not supplied, the geocoder will attempt to use the native language of the domain from which the request is sent wherever possible.

region — The region code, specified as a ccTLD ("top-level domain") two-character value. This parameter will only influence, not fully restrict, results from the geocoder. (For more information see Region Biasing below.)

components — The component filters, separated by a pipe (|). Each component filter consists of a component:value pair and will fully restrict the results from the geocoder. For more information see Component Filtering, below.)

Upvotes: 2

Related Questions