Reputation: 41
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...
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
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®ion=au&locationbias=circle:[email protected],134.537373&key=VALID_KEY
Upvotes: 3