无法解决:Geocode未成功,原因如下:OVER_QUERY_LMIT



我想使用一个简单的JavaScript文件在gGogle地图上打印多个引脚。我得到了这个错误:

Geocode未成功,原因如下:OVER_QUERY_LIMIT

然后我在谷歌上搜索了一下,基本上有两个可能的问题:

  1. API密钥未为地理定位启用
  2. 到每秒许多查询

好吧,我的api启用了地理定位,每次迭代后我都添加了一个sleep函数。

这个错误有点奇怪,因为它…停止了,然后又重新启动。。。我在代码中添加了评论:

//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 */ } 
}

知道吗?

谢谢xomena,我已经解决了它。对于那些有同样问题的人,这里是解决方案:

我的错误是sleep()函数的位置和它的行为方式。这里有一个新的小问题:当它添加新的标记时,地图开始倾斜、移动、缩放和颤动,所以用户体验很糟糕。但我仍然没有找到解决方案。

代码:

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

相关内容

  • 没有找到相关文章

最新更新