Reputation: 1
I have a customer database in Google Sheets with a list of addresses. I'm using Geocoding to generate their longitude/latitude for easy use in Fusion Tables for mapping. I used the following code only one time, on a list of 963 customers, and it stopped on customer 761 with the "Service invoked too many times for one day: geocode" error.
I am aware of the 2,500 daily limit for calling the Geocode API. What confuses me is that I'm not certain how this code reached that limit after 761 iterations. I've looked over it time and again and don't see how it's performing more than 1 request per iteration. I have no other similar projects and have never used the Geocode API feature until now. I'd like to prevent accidentally reaching this error in the future.
Where have I gone wrong?
function geocodeFirstAddressFromCell(){
var sss = SpreadsheetApp.openById('WORKBOOK ID HERE');
var ss = sss.getSheetByName('Sheet1');
var lastRow = ss.getLastRow();
for (var startRow = 2; startRow < lastRow; startRow++) {
var addressCellData = ss.getRange(startRow, 13).getValue();
var results = Maps.newGeocoder().geocode(addressCellData);
if (results.status == 'OK') {
var bestResult = results.results[0];
var lat = bestResult.geometry.location.lat;
var lng = bestResult.geometry.location.lng;
var latLng = lat + ',' + lng;
var addressTargetCellData = ss.getRange(startRow, 16).setValue(latLng);
ss.getRange(startRow, 17).setValue(1);
} else {
Logger.log(results.status);
return '0,0';
}
}
}
Upvotes: 0
Views: 306
Reputation: 4840
The first thing to check is whether you are using an API key, because otherwise you can hit the OVER_QUERY_LIMIT error quite easily. For some tips on how to start using API keys, see https://issuetracker.google.com/116675310#comment2
Upvotes: 1
Reputation: 348
Maps.newGeocoder().geocode(addressCellData);
Without much knowledge of this library, are you sure that the previous code is not making more than one request to the API?
If this is the problem, I think that the appropriate way is the following:
var geocoder = Maps.newGeocoder();
for (...) {
var results = geocoder.geocode(addressCellData);
}
I hope you find it useful.
Upvotes: 0