1
votes

I am trying to parse JSON results from a Google Place API call using Google Scripts and URLFetchApp.

I have enabled Places API and created an API key and if I paste the following into Chrome browser...

https://maps.googleapis.com/maps/api/place/findplacefromtext/json?input=telstra%20headoffice&inputtype=textquery&fields=formatted_address&key=VALID_KEY

I receive:

{
   "candidates" : [
      {
         "formatted_address" : "240 Exhibition St, Melbourne VIC 3000, Australia"
      }
   ],
   "status" : "OK"
}

However, when I try to retrieve it using a Google Script...

function mapAddress() {
 var url = "https://maps.googleapis.com/maps/api/place/findplacefromtext/json?input=telstra%20headoffice&inputtype=textquery&fields=formatted_address&key=VALID_KEY";
 var response = UrlFetchApp.fetch(url,{muteHttpExceptions:true});
 var json = response.getContentText();
 var data = JSON.parse(json);
 Logger.log(data.status);
 Logger.log(response);

When I review the execution transcript this is what's reported...

[19-12-13 10:43:50:300 AEDT] Starting execution
[19-12-13 10:43:50:666 AEDT] UrlFetchApp.fetch([https://maps.googleapis.com/maps/api/place/findplacefromtext/json?input=telstra%20headoffice&inputtype=textquery&fields=formatted_address&key=VALID_KEY, {muteHttpExcepti...) [0.354 seconds]
[19-12-13 10:43:50:666 AEDT] UrlFetchApp.HTTPResponse.getContentText() [0 seconds]
[19-12-13 10:43:50:668 AEDT] Logger.log([ZERO_RESULTS, []]) [0 seconds]
[19-12-13 10:43:50:669 AEDT] Logger.log([{
   "candidates" : [],
   "status" : "ZERO_RESULTS"
}
, []]) [0 seconds]
[19-12-13 10:43:50:670 AEDT] Execution succeeded [0.36 seconds total runtime]

What am I doing wrong?

1
I tried to use the URL you are providing in my Chrome Browser, but I get the same result ZERO_RESULTS, you should probably check again that URL before using it in Apps Script. - alberto vielma
It must have something to do with region or location of the search. I am based in Australia. The Google Script will be executing from a GCP location. - ezascanbe
I revisited the documentation for Place Search and there is a locationbias field. It also states that the default is based on IP address of the originating query! Including a location bias around the centre of Australia resolved the problem... maps.googleapis.com/maps/api/place/findplacefromtext/… - ezascanbe

1 Answers

3
votes

The Places API documentation includes information about the locationbias. By default it uses the IP address of the originating request.

https://developers.google.com/places/web-service/search#PlaceSearchRequests

I used a locationbias based on the lat/log of Australia and it now works.

https://maps.googleapis.com/maps/api/place/findplacefromtext/json?input=telstra%20headoffice&inputtype=textquery&fields=place_id,formatted_address&region=au&locationbias=circle:[email protected],134.537373&key=VALID_KEY