我正在使用Google Maps API在地图上显示大约50个位置。我正在使用客户端地理编码。我正在使用window.setTimeout来控制应用程序每秒发送的地理编码请求的数量。如果我每秒发送多个请求,则收到OVER QUERY LIMIT响应。
问题:这个限制不应该是每秒10个查询吗?如果是,那我可能做错了什么?如果否,那么Business API每秒的查询限制是否更大?
请注意,我们的应用程序每天不会达到25,000个查询。
地理编码器具有配额和费率限制。根据经验,您可以对约10个位置进行地理编码,而不会达到查询限制(实际数量可能取决于服务器负载)。最好的解决方案是延迟出现OVER_QUERY_LIMIT错误的时间,然后重试。
通常,当您需要在地图上显示很多点时,最好使用服务器端方法,本文介绍了何时使用每个点:
地理编码策略:https : //developers.google.com/maps/articles/geocodestrat
客户端限制不完全是“每秒10个请求”,并且由于API文档中没有对此进行说明,因此我不会依赖于它的行为。
由于Google服务器过载,此方法不正确。有关更多信息,请参见 https://gis.stackexchange.com/questions/15052/how-to-avoid-google-map-geocode-limit#answer-15365
顺便说一句,如果您仍然希望继续进行操作,可以在这里找到一个代码,该代码可让您加载来自Google地图的多个标记ajax,从而避免OVER_QUERY_LIMIT错误。
我已经在onw服务器上进行了测试,并且可以正常工作!:
var lost_addresses = [];
geocode_count = 0;
resNumber = 0;
map = new GMaps({
div: '#gmap_marker',
lat: 43.921493,
lng: 12.337646,
});
function loadMarkerTimeout(timeout) {
setTimeout(loadMarker, timeout)
}
function loadMarker() {
map.setZoom(6);
$.ajax({
url: [Insert here your URL] ,
type:'POST',
data: {
"action": "loadMarker"
},
success:function(result){
/***************************
* Assuming your ajax call
* return something like:
* array(
* 'status' => 'success',
* 'results'=> $resultsArray
* );
**************************/
var res=JSON.parse(result);
if(res.status == 'success') {
resNumber = res.results.length;
//Call the geoCoder function
getGeoCodeFor(map, res.results);
}
}//success
});//ajax
};//loadMarker()
$().ready(function(e) {
loadMarker();
});
//Geocoder function
function getGeoCodeFor(maps, addresses) {
$.each(addresses, function(i,e){
GMaps.geocode({
address: e.address,
callback: function(results, status) {
geocode_count++;
if (status == 'OK') {
//if the element is alreay in the array, remove it
lost_addresses = jQuery.grep(lost_addresses, function(value) {
return value != e;
});
latlng = results[0].geometry.location;
map.addMarker({
lat: latlng.lat(),
lng: latlng.lng(),
title: 'MyNewMarker',
});//addMarker
} else if (status == 'ZERO_RESULTS') {
//alert('Sorry, no results found');
} else if(status == 'OVER_QUERY_LIMIT') {
//if the element is not in the losts_addresses array, add it!
if( jQuery.inArray(e,lost_addresses) == -1) {
lost_addresses.push(e);
}
}
if(geocode_count == addresses.length) {
//set counter == 0 so it wont's stop next round
geocode_count = 0;
setTimeout(function() {
getGeoCodeFor(maps, lost_addresses);
}, 2500);
}
}//callback
});//GeoCode
});//each
};//getGeoCodeFor()
例:
map = new GMaps({
div: '#gmap_marker',
lat: 43.921493,
lng: 12.337646,
});
var jsonData = {
"status":"success",
"results":[
{
"customerId":1,
"address":"Via Italia 43, Milano (MI)",
"customerName":"MyAwesomeCustomer1"
},
{
"customerId":2,
"address":"Via Roma 10, Roma (RM)",
"customerName":"MyAwesomeCustomer2"
}
]
};
function loadMarkerTimeout(timeout) {
setTimeout(loadMarker, timeout)
}
function loadMarker() {
map.setZoom(6);
$.ajax({
url: '/echo/html/',
type: "POST",
data: jsonData,
cache: false,
success:function(result){
var res=JSON.parse(result);
if(res.status == 'success') {
resNumber = res.results.length;
//Call the geoCoder function
getGeoCodeFor(map, res.results);
}
}//success
});//ajax
};//loadMarker()
$().ready(function(e) {
loadMarker();
});
//Geocoder function
function getGeoCodeFor(maps, addresses) {
$.each(addresses, function(i,e){
GMaps.geocode({
address: e.address,
callback: function(results, status) {
geocode_count++;
console.log('Id: '+e.customerId+' | Status: '+status);
if (status == 'OK') {
//if the element is alreay in the array, remove it
lost_addresses = jQuery.grep(lost_addresses, function(value) {
return value != e;
});
latlng = results[0].geometry.location;
map.addMarker({
lat: latlng.lat(),
lng: latlng.lng(),
title: e.customerName,
});//addMarker
} else if (status == 'ZERO_RESULTS') {
//alert('Sorry, no results found');
} else if(status == 'OVER_QUERY_LIMIT') {
//if the element is not in the losts_addresses array, add it!
if( jQuery.inArray(e,lost_addresses) == -1) {
lost_addresses.push(e);
}
}
if(geocode_count == addresses.length) {
//set counter == 0 so it wont's stop next round
geocode_count = 0;
setTimeout(function() {
getGeoCodeFor(maps, lost_addresses);
}, 2500);
}
}//callback
});//GeoCode
});//each
};//getGeoCodeFor()
#gmap_marker {
min-height:250px;
height:100%;
width:100%;
position: relative;
overflow: hidden;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="http://maps.google.com/maps/api/js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gmaps.js/0.4.24/gmaps.min.js" type="text/javascript"></script>
<div id="gmap_marker"></div> <!-- /#gmap_marker -->
举报