我知道有很多关于这个问题,但我不能找到一个回答我的具体问题。
有谁能够看到的东西根本不对的(有很可能是/一定要!)?从我的理解,对谷歌的地理编码API每秒的极限是每秒50个请求。我只有约80个地址运行下面的代码,并将其约15。15个请求不会在1取得第二,为什么我收到OVER_QUERY_LIMIT后抛出OVER_QUERY_LIMIT的错误?
我设置间隔时间是因为遇到问题。我对这个间隔的理解是,它应该将每秒的查询数限制为4(远远少于50)。但是我仍然面对这个问题!
var done = true;
function geocodeAddress(address, geocoder, resultsMap) {
done = false;
geocoder.geocode({"address": address}, function(results, status) {
if (status === "OK") {
var marker = new google.maps.Marker({
map: resultsMap,
position: results[0].geometry.location
});
} else {
console.error("Geocode on '" + address + "' was not successful for the following reason: ");
console.log(status);
}
done = true;
});
}
function geocodeAll(addresses) {
var addressCount = addresses.length;
var counter = 0;
var next = setInterval(function() {
console.log(counter);
if(done) {
geocodeAddress(addresses[counter], geocoder, map);
counter++;
}
if(!(counter < addressCount)) {
clearInterval(next);
}
}, 250);
}
正如@Mike基恩说,你应该通过网络服务进行地址解析地址。
下面是一个小演示。
import Axios from 'axios';
Axios.get('https://maps.googleapis.com/maps/api/geocode/json', {
params: {
address,
key: 'API_KEY'
}
}).then(res => {
if (res.data.status === 'OK') {
console.log(res.data.results[0])
} else {
console.log(res.data.status)
}
}).catch(error => {
console.log(error)
})