1
votes

I get this error message when using the google map geocode function, which I understand happens when I exceed my free limit of 2500 requests in a day. However I've set up a billing option to pay the extra for additional requests, but I still get this error. When I set up billing, it asked me to create a project that's then associated with my billing info, which I did and I then got an API key to use.

I don't know whether / where I would put this API key in my code. Here's a snippet of my code

 var addressColumn = 1;
  var addressRow;

  var latColumn = addressColumn + 1;
  var lngColumn = addressColumn + 2;

  var geocoder = Maps.newGeocoder().setRegion(getGeocodingRegion());
  var location;

  for (addressRow = 1; addressRow <= cells.getNumRows(); ++addressRow) {
    var address = cells.getCell(addressRow, addressColumn).getValue();

    // Geocode the address and plug the lat, lng pair into the 
    // 2nd and 3rd elements of the current range row.
    location = geocoder.geocode(address);

    // Only change cells if geocoder seems to have gotten a 
    // valid response.
    if (location.status == 'OK') {
      lat = location["results"][0]["geometry"]["location"]["lat"];
      lng = location["results"][0]["geometry"]["location"]["lng"];

      cells.getCell(addressRow, latColumn).setValue(lat);
      cells.getCell(addressRow, lngColumn).setValue(lng);
    }
  }
};

Any ideas how to link my paid billing project to this code so I can increase my usage of the API?

1
Did you look here ?Pierre-Marie Richard

1 Answers

1
votes

You must trigger this on sheet changes, otherwise this function trigger too many time. Also I have add a timer for mitigate the request for second rate limit.

function getLatLng(address) {
        if(address=="")return("");
        var API_KEY = "YOUR API KEY";

        var options = {
            muteHttpExceptions: true,
            contentType: "application/json",
        };


            var serviceUrl = "https://maps.googleapis.com/maps/api/geocode/json?address=" + address + "&key=" + API_KEY;
      try{
            var response = UrlFetchApp.fetch(serviceUrl, options);

            //sleep random between 2 and 1 second for request
            var milliseconds = Math.floor(((Math.random() * 2) + 1) * 1000);  
            Utilities.sleep(milliseconds);

            if (response.getResponseCode() == 200) {
                var location = JSON.parse(response.getContentText());

                if (location["status"] == "OK") {
                    var lat = location["results"][0]["geometry"]["location"]["lat"];
                    var lng = location["results"][0]["geometry"]["location"]["lng"];

                     return([[lat,lng]]);
                }
              else{
                return([["error",location["status"]]]);
              }
            }
      }
      catch(err){
        return([["error",err.message]]);
      }
    };

You can get an api key here. You can use this function directly in the sheet cell ex: getLatLng(D2)