Can not solve: Geocode was not successful for the following reason: OVER_QUERY_LIMIT

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);
}

Leave a Comment