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:
- API key not enabled for geolocation
- 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?