ezascanbe
ezascanbe

Reputation: 41

Places API Call via Google Scripts returns ZERO_RESULTS

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?

Upvotes: 1

Views: 482

Answers (1)

ezascanbe
ezascanbe

Reputation: 41

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

Upvotes: 3

Related Questions