1

Since Google requires recently to add your key to API uses, I have a problem to get it run on a script that was calculating distances properly before. In my code I have this:

<cfhttp url="http://maps.googleapis.com/maps/api/geocode/xml" method="GET">

so if I add the API key like this (according to official documentation):

<cfhttp url="http://maps.googleapis.com/maps/api/geocode/xml?key=MyKeyGoesHere" method="GET">

ColdFusion is telling me there is an error:

Element GEOCODERESPONSE.RESULT.GEOMETRY.LOCATION.LAT.XMLTEXT is undefined in MYXMLDOC.

I got the same error message, when I tried to add the key via cfhttpparam:

<cfhttpparam type="FORMFIELD" name="key" value="MyKeyGoesHere" />

Here is the relevant part of the script without API key:

<cfset address = '#usersearch#,  DE'>
<cfhttp url="http://maps.googleapis.com/maps/api/geocode/xml" method="GET">
<cfhttpparam type="FORMFIELD" name="address" value="#address#" />
<cfhttpparam type="FORMFIELD" name="sensor" value="false" />
</cfhttp> 
<cfset myXMLDoc = XMLParse(cfhttp.fileContent) />
<cfif myXMLDoc contains 'ZERO_RESULTS' or myXMLDoc contains 'OVER_QUERY_LIMIT'>
We have encountered an error, please contact the webmaster
<cfelse>
<cfset latitude = '#myXMLDoc.GeocodeResponse.result.geometry.location.lat.XmlText#'>
<cfset longitude = '#myXMLDoc.GeocodeResponse.result.geometry.location.lng.XmlText#'>

I don't know where else to set the API key...?

ITfee
  • 31
  • 4
  • 1
    Could you dump the cfhttp response . I think there is a problem with the restrictions on your api key. The following post may be helpful https://stackoverflow.com/a/42451277/2482184 . But without more info this is just a guess. – Nebu Oct 11 '18 at 13:05
  • Any particular version of ColdFusion? – James A Mohler Oct 11 '18 at 14:49
  • It was indeed a restriction problem with the api key like mentioned by @Nebu The response was "API keys with referer restrictions cannot be used with this API." I changed it to IP restriction and now the script works fine again. Thank you! – ITfee Oct 11 '18 at 15:14

0 Answers0