-1

I want to print multiple pins on gGogle map using a simple JavaScript file. I got this error:

Geocode was not successful for the following reason: OVER_QUERY_LIMIT

Then I googled and basically there are 2 possible problems:

  1. API key not enabled for geolocation
  2. to many queries per second

Well, my api is enabled for geolocation and I added a sleep function after every iteration.

There is something weird in this error because it... stops and then it restarts again... I added comments in code:

//arrays of addresses
var addressesFoodAndDrink = [               // all these addresses are printed
    "fuori stile, riva del garda", 
    "agraria, riva del garda", 
    "officina del panino, riva del garda",
    "le foci di rita, varone, riva el garda",
    "le servite, san giorgio, tn",
    "la caneva bistrot, riva del garda",
    "la colombera, riva del garda",
    "ristorante panorama, pregasine"
];

var addressesShopping = [
    "poli, arco, trento",       // printed
    "despar, arco",             // printed
    "lidl, riva del garda",     // printed
    "penny, arco"               // error  !!!!
];

var addressesHome = ["pause lake garda, san giorgio"];  // printed

// icons
var iconFoodAndDrink = "food.png";
var iconHome = "home.png";
var iconShopping = "shopping.png";


function codeAddress(geocoder, map, addresses, icon) {
    
    // sleep in order to avoid to many queries
    sleep(2000);

        addresses.forEach(function (item, index, array){
            
            // ADDED SLEEP 
            sleep(200);
            
             geocoder.geocode({'address': item}, function(results, status) {
              if (status === 'OK') {
                map.setCenter(results[0].geometry.location);
                map.setZoom(13);
                var marker = new google.maps.Marker({
                  map: map,
                  position: results[0].geometry.location,
                  icon: icon
                });
                  console.log("gocoder ok");
              } else {
                alert('Geocode was not successful for the following reason: ' + status);
               }
            });          
        })
}


function initMap() {
    
    var geocoder;
    geocoder = new google.maps.Geocoder();
    
    var map = new google.maps.Map(document.getElementById('map'), {zoom: 14, center:  {lat: 45.8993532, lng: 10.8666372}});
    
    // geocoding the first array
    codeAddress(geocoder, map, addressesFoodAndDrink, iconFoodAndDrink);
    
    // geocoding the second array with error
    codeAddress(geocoder, map, addressesShopping, iconShopping);    // The error is generated here, at the forth element which is not printed
    
    // geocoding the first array
    codeAddress(geocoder, map, addressesHome, iconHome); // but this is printed, wired
}

function sleep(s){
  var now = new Date().getTime();
  while(new Date().getTime() < now + (s)){ /* do nothing */ } 
}

any idea?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
bog
  • 1,323
  • 5
  • 22
  • 34
  • Please read the [docs](https://developers.google.com/maps/premium/previous-licenses/articles/usage-limits#problems). – MrUpsidown Jul 18 '20 at 20:07
  • 1
    Similar question: https://stackoverflow.com/questions/57511554/async-and-await/57513095. Try to apply Exponential Backoff approach as explained there. – xomena Jul 18 '20 at 23:38
  • @xomena, thanks it worked. – bog Jul 19 '20 at 06:35

1 Answers1

2

Thanks xomena, I have solved it. For those who are having the same problem, here is the solution:

My error was the position of sleep() function and the way it acts. Here is a new small problem: as it adds new markers the map starts tilting, shifting, zooming and trilling so the user experience is terrible. But I still did not look up for a solution for this.

Code:

//arrays of addresses
var addressesFoodAndDrink = [               
    "fuori stile, riva del garda", 
    "agraria, riva del garda", 
    "officina del panino, riva del garda",
    "le foci di rita, varone, riva el garda",
    "le servite, san giorgio, tn",
    "la caneva bistrot, riva del garda",
    "la colombera, riva del garda",
    "ristorante panorama, pregasine"
];

var addressesShopping = [
    "poli, arco, trento",       
    "despar, arco",             
    "lidl, riva del garda",     
    "torbole, trento"               
];

var addressesHome = ["pause lake garda, san giorgio, Trento"];  

// icons
var iconFoodAndDrink = "food.png";
var iconHome = "home.png";
var iconShopping = "shopping.png";

var delayFactor = 0;

function codeAddress(geocoder, map, addresses, icon) {

        addresses.forEach(function (item, index, array){
            
             geocoder.geocode({'address': item}, function(results, status) {
              if (status === 'OK') {
                map.setCenter(results[0].geometry.location);
                map.setZoom(13);
                var marker = new google.maps.Marker({
                  map: map,
                  position: results[0].geometry.location,
                  icon: icon
                });
                  console.log("gocoder ok for " + item + " - Removing it");
                  const index = addresses.indexOf(item);
                    if (index > -1) {
                      addresses.splice(index, 1);
                        console.log("new dim of array: " + addresses)
                    }
              } else if (status === google.maps.GeocoderStatus.OVER_QUERY_LIMIT){
                    console.log("Waiting for Limit for item: "+ item);
                  
                    delayFactor++;
                    setTimeout(function () {
                           codeAddress(geocoder, map, addresses, icon)
                    }, delayFactor * 1100);
               } else {
                   console.log("errore diverso: " + status);
               }
            });          
        })
}


function initMap() {
    
    var geocoder;
    geocoder = new google.maps.Geocoder();
    
    var map = new google.maps.Map(document.getElementById('map'), {zoom: 14, center:  {lat: 45.8993532, lng: 10.8666372}});
    
    codeAddress(geocoder, map, addressesFoodAndDrink, iconFoodAndDrink);

    codeAddress(geocoder, map, addressesShopping, iconShopping);    
    
    codeAddress(geocoder, map, addressesHome, iconHome);
}
bog
  • 1,323
  • 5
  • 22
  • 34
  • The issue with the shifting of the map is because you set the center with each marker you place. – AsheraH Nov 05 '21 at 09:59