Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extracting Just The City From Google Geocode API Using Coldfusion

Status
Not open for further replies.

3dColor

Programmer
Jan 10, 2006
240
US
I have users that input data that is sometimes is not correct. For instance they might misspell their city or they might write 'St John' instead of 'St. John'. I would rather not dirty up my database so I would like to check with Google to see if what the user inputted was correct.

How do I extract the city 'Mountain View' from the below code? I think I am getting close but I am not good at parsing XML.

Code:
<cfhttp
	url="[URL unfurl="true"]http://maps.googleapis.com/maps/api/geocode/xml?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true"[/URL]
	method="GET" resolveurl="No" timeout="30" throwonerror="yes">
</cfhttp>
<cfset xmlDoc = XmlParse(CFHTTP.FileContent)>

<cfloop
    index="address_component"
    from="1"
    to="#ArrayLen( xmlDoc.GeocodeResponse.XmlChildren )#"
    step="1">   
    
    <!--- Get a short hand for the current rentalad node. --->
    <cfset xmladdress_component = xmlDoc.GeocodeResponse[ "address_component" ][ address_component ] />           
    <!--- Get the contactinfo children. --->
    <cfset xmlLong_name = xmladdress_component[ "long_name" ] />

	<cfoutput> 
        #xmlAddress_component.long_name.xmltext#<br />     
    </cfoutput> 

</cfloop>

<CFDUMP var="#xmlDoc#">
 
I got it working with the help of Ray Camden:

<cfhttp
url=" method="GET" resolveurl="No" timeout="30" throwonerror="yes">
</cfhttp>
<cfset xmlDoc = XmlParse(CFHTTP.FileContent)>

<cfloop
index="address_component"
from="1"
to="#ArrayLen( xmlDoc.GeocodeResponse.result.address_component )#"
>

<!--- Get a short hand for the current rentalad node. --->
<cfset xmladdress_component = xmlDoc.GeocodeResponse.result["address_component" ][ address_component ] />
<!--- Get the contactinfo children. --->
<cfset xmlLong_name = xmladdress_component[ "long_name" ] />

<cfif xmlAddress_component.type.xmltext EQ "locality">
<cfoutput>#xmlAddress_component.long_name.xmltext#<br /></cfoutput>
</cfif>

</cfloop>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top