Google Maps API v3中的OVER_QUERY_LIMIT:如何暂停/延迟Javascript以减慢速度? [英] OVER_QUERY_LIMIT in Google Maps API v3: How do I pause/delay in Javascript to slow it down?

查看:246
本文介绍了Google Maps API v3中的OVER_QUERY_LIMIT:如何暂停/延迟Javascript以减慢速度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了这些论坛讨论得很好的问题,但是没有任何建议似乎对我有用,所以我正在寻找一些完整的javascript,它在保存为html文件时起作用。



问题是,当我尝试使用JavaScript调用的V3 API在Google Map上对地理编码> 11个地点进行地理编码时,我一直都遇到OVER_QUERY_LIMIT错误。我知道您可以调用地理编码器的速率有限制(以及总量的每日限制),所以我需要在数组中的每个结果之间引入一个暂停。



非常感谢任何帮助。



以下是我的代码:

 < script type =text / javascriptsrc =http://maps.google.com/maps/api/js?sensor=false>< / script> 
< script type =text / javascript>
var geocoder;
var map;
var wait = false;


函数initialize(){
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(51.32,0.5);



var myOptions = {
zoom:8,
center:latlng,
mapTypeId:google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById(map_canvas),myOptions);
codeAddress('KT16 8LA'+',UK');
codeAddress('LS8 2LQ'+',UK');
codeAddress('NE13 8AF'+',UK');
codeAddress('KT12 2BE'+',UK');
codeAddress('W1W 8AN'+',UK');
codeAddress('EC3N 2LS'+',UK');
codeAddress('BS9 3BH'+',UK');
codeAddress('KA10 6LZ'+',UK');
codeAddress('EC1V 9BW'+',UK');
codeAddress('WD18 8YN'+',UK');
codeAddress('HA3 6DQ'+',UK');
codeAddress('W1U 3PL'+',UK');
codeAddress('W1T 7QL'+',UK');
codeAddress('W1S 1TD'+',UK');
codeAddress('SW1X 8NX'+',UK');
codeAddress('LE2 8ET'+',UK');
codeAddress('BA3 4BH'+',UK');
codeAddress('AL3 8JP'+',UK');
codeAddress('DE55 4QJ'+',UK');
codeAddress('W6 0QT'+',UK');
codeAddress('LA1 1PP'+',UK');
codeAddress('SW16 4DH'+',UK');
codeAddress('WC2N 6DF'+',UK');
codeAddress('RM6 6LS'+',UK');
codeAddress('S25 3QZ'+',UK');
codeAddress('WC2H 7LR'+',UK');
codeAddress('BH24 1DW'+',UK');
codeAddress('EC2N 6AR'+',UK');
codeAddress('W1U 2FA'+',UK');
codeAddress('B60 3DX'+',UK');


函数codeAddress(vPostCode){
if(geocoder){
geocoder.geocode({'address':''+ vPostCode +' },function(results,status){
if(status == google.maps.GeocoderStatus.OK){
map.setCenter(results [0] .geometry.location);
var marker = new google.maps.Marker({
map:map,
position:results [0] .geometry.location
});
} else {
警报(地理编码不成功,原因如下:+状态);
}
});
}
}

< / script>
< body style =margin:0px; padding:0px;的onload = 初始化() >
< div id =map_canvasstyle =width:100%; height:90%>< / div>
< / body>

编辑:这是我试图让它暂停/等待相关部分,但它什么都不做:

 函数codeAddress(vPostCode){
if(geocoder){
while(wait){/ *等待。 * /};
geocoder.geocode({'address':''+ vPostCode +'},function(results,status){
if(status == google.maps.GeocoderStatus.OK){
map.setCenter(results [0] .geometry.location);
var marker = new google.maps.Marker({
map:map,
position:results [0] .geometry.location
});
/ *当地理编码失败时,查看它是否是因为超过配额错误:* /
} else if(status == google.maps。 GeocoderStatus.OVER_QUERY_LIMIT){
wait = true;
setTimeout(wait = true,2000);
// alert(OQL:+ status);
} else {
alert(由于以下原因Geocode不成功:+ status;
}
});
}
}


解决方案

像这两行出现在Mike Williams的教程中:
$ b $ pre $ wait = true;
setTimeout(wait = true,2000);

以下是第3版端口:

http://acleach.me.uk/gmaps/v3/plotaddresses.htm



相关位代码是

  // === ===地理编码====== 
function getAddress(search,next){
geo.geocode({address:search},function(results,status)
{
//如果成功
if(status == google.maps.GeocoderStatus.OK){
//假设第一个标记是我们想要的标记
var p = result [0] .geometry.location;
var lat = p.lat();
var lng = p.lng();
//输出数据
var msg = 'address =''+ search +''lat ='+ lat +'lng ='+ lng +'(delay ='+ delay +'ms)< br>';
document.getElementById(messages)。 innerHTML + = msg;
//创建一个记号
createMarker(搜索,纬度,经度);
}
// ======解码错误状态======
else {
// ===如果我们发送请求的速度很快,再试一次,并增加延迟
if(status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT){
nextAddress--;
delay ++;
} else {
var reason =Code+ status;
var msg ='address =''+ search +'error ='+ reason +'(delay ='+ delay +'ms)< br>';
document.getElementById(messages)。innerHTML + = msg;
}
}
next();
}
);
}


I'm hitting an issue that is WELL discussed in these forums, but none of the recommendations seem to be working for me so I'm looking for some full javascript that works when saved as an html file.

The issue is I keep hitting the OVER_QUERY_LIMIT error when trying to geocode > 11 locations on a Google Map using the V3 APIs called by Javascript. I understand that there is a limit to the rate at which you can call the geocoder (as well as the daily limit on total volume), so I need to introduce a pause in between each result in the array.

Any help very much appreciated.

Here is my code:

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var geocoder;
var map;
var wait = false;


  function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(51.32, 0.5);



var myOptions = {
  zoom: 8,
  center: latlng,
  mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
codeAddress('KT16 8LA' + ', UK');
codeAddress('LS8 2LQ' + ', UK');
codeAddress('NE13 8AF' + ', UK');
codeAddress('KT12 2BE' + ', UK');
codeAddress('W1W 8AN' + ', UK');
codeAddress('EC3N 2LS' + ', UK');
codeAddress('BS9 3BH' + ', UK');
codeAddress('KA10 6LZ' + ', UK');
codeAddress('EC1V 9BW' + ', UK');
codeAddress('WD18 8YN' + ', UK');
codeAddress('HA3 6DQ' + ', UK');
codeAddress('W1U 3PL' + ', UK');
codeAddress('W1T 7QL' + ', UK');
codeAddress('W1S 1TD' + ', UK');
codeAddress('SW1X 8NX' + ', UK');
codeAddress('LE2 8ET' + ', UK');
codeAddress('BA3 4BH' + ', UK');
codeAddress('AL3 8JP' + ', UK');
codeAddress('DE55 4QJ' + ', UK');
codeAddress('W6 0QT' + ', UK');
codeAddress('LA1 1PP' + ', UK');
codeAddress('SW16 4DH' + ', UK');
codeAddress('WC2N 6DF' + ', UK');
codeAddress('RM6 6LS' + ', UK');
codeAddress('S25 3QZ' + ', UK');
codeAddress('WC2H 7LR' + ', UK');
codeAddress('BH24 1DW' + ', UK');
codeAddress('EC2N 6AR' + ', UK');
codeAddress('W1U 2FA' + ', UK');
codeAddress('B60 3DX' + ', UK');    
}

  function codeAddress(vPostCode) {
if (geocoder) {
  geocoder.geocode( { 'address': "'" + vPostCode + "'"}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      map.setCenter(results[0].geometry.location);
      var marker = new google.maps.Marker({
          map: map, 
          position: results[0].geometry.location
      });
    } else {
      alert("Geocode was not successful for the following reason: " + status);
    }
  });
}
}

</script>
<body style="margin:0px; padding:0px;" onload="initialize()">
<div id="map_canvas" style="width:100%; height:90%"></div>
</body>

EDIT: This is what I've tried to do to get it to pause/wait in the relevant section, but it doesn't do anything:

function codeAddress(vPostCode) {
    if (geocoder) {
    while (wait) { /* Just wait. */ };
      geocoder.geocode( { 'address': "'" + vPostCode + "'"}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          map.setCenter(results[0].geometry.location);
          var marker = new google.maps.Marker({
              map: map, 
              position: results[0].geometry.location
          });
        /* When geocoding "fails", see if it was because of over quota error: */
        } else if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT) { 
        wait = true;
        setTimeout("wait = true", 2000);
        //alert("OQL: " + status);
        } else {
          alert("Geocode was not successful for the following reason: " + status);
        }
      });
    }
  }

解决方案

Nothing like these two lines appears in Mike Williams' tutorial:

    wait = true;
    setTimeout("wait = true", 2000);

Here's a Version 3 port:

http://acleach.me.uk/gmaps/v3/plotaddresses.htm

The relevant bit of code is

  // ====== Geocoding ======
  function getAddress(search, next) {
    geo.geocode({address:search}, function (results,status)
      { 
        // If that was successful
        if (status == google.maps.GeocoderStatus.OK) {
          // Lets assume that the first marker is the one we want
          var p = results[0].geometry.location;
          var lat=p.lat();
          var lng=p.lng();
          // Output the data
            var msg = 'address="' + search + '" lat=' +lat+ ' lng=' +lng+ '(delay='+delay+'ms)<br>';
            document.getElementById("messages").innerHTML += msg;
          // Create a marker
          createMarker(search,lat,lng);
        }
        // ====== Decode the error status ======
        else {
          // === if we were sending the requests to fast, try this one again and increase the delay
          if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
            nextAddress--;
            delay++;
          } else {
            var reason="Code "+status;
            var msg = 'address="' + search + '" error=' +reason+ '(delay='+delay+'ms)<br>';
            document.getElementById("messages").innerHTML += msg;
          }   
        }
        next();
      }
    );
  }

这篇关于Google Maps API v3中的OVER_QUERY_LIMIT:如何暂停/延迟Javascript以减慢速度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆