当前位置: 首页 > 面试题库 >

Google Maps API v3中的OVER_QUERY_LIMIT:如何在Javascript中暂停/延迟以降低速度?

应煌
2023-03-14
问题内容

我遇到的问题在这些论坛上都进行了很好的讨论,但是这些建议似乎都不对我有用,因此我正在寻找一些保存为html文件时可以使用的完整javascript。

问题是,当尝试使用Javascript调用的V3 API对Google
Map上的11个以上的位置进行地理编码时,我一直遇到OVER_QUERY_LIMIT错误。我了解您可以致电地理编码器的速率受到限制(以及每日总体积限制),因此我需要在数组中每个结果之间引入一个暂停。

任何帮助非常感谢。

这是我的代码:

<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>

编辑:这是我试图使它在相关部分中暂停/等待的方式,但是它没有做任何事情:

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

问题答案:

迈克·威廉姆斯的教程中没有出现这两行:

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

这是第3版端口:

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

相关的代码位是

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


 类似资料:
  • 问题内容: 我正在寻找有关如何在低延迟至关重要的环境中最大程度地调整年轻一代(相对于老一代)的争论。 我自己的测试倾向于表明,当年轻一代相当大时(例如-XX:NewRatio <3),延迟是最低的,但是我不能直觉地认为年轻一代越大,进行垃圾处理的时间就越多收集。 该应用程序在Linux 64位jdk 6上运行。 内存使用量大约是启动时加载的50万兆个长寿命对象(=数据缓存),并且从那里仅创建(很多

  • 问题内容: 我的网站上有这片js可以切换图像,但是第二次单击图像时需要延迟。延迟应为1000ms。因此,您将单击img.jpg,然后将出现img_onclick.jpg。然后,您将单击img_onclick.jpg图像,然后应该有1000ms的延迟,然后才能再次显示img.jpg。 这是代码: 问题答案: 用途 :

  • 问题内容: 我想在循环内添加延迟/睡眠: 我这样尝试过: 只有第一种情况是正确的:显示后,它将等待3秒钟,然后显示,但随后将不断重复。 我想要的是在显示3秒之后显示出来,然后它需要第二次等待3秒,依此类推。 问题答案: 该函数是非阻塞的,将立即返回。因此,您的循环将非常快速地迭代,并且将快速连续地发起3秒超时触发。这就是为什么您的第一个警报会在3秒钟后弹出,而其余所有警报都将连续不断地出现。 您可

  • 问题内容: 我有一个textarea,人们可以自然地输入一些文本,因此我想这样做,以便不时提出AJAX请求,以获取有关textarea的内容的一些建议(例如堆栈溢出的Related Questions,但是对于textarea,而不是文本输入)。问题是我不能在每次按键时都发出AJAX请求(这是无用的,而且非常消耗资源),而且我不确定这样做的最有效方法是什么(每X字?每X秒一次) ? 或者是其他东西

  • 问题内容: 因此,在这段代码中: 似乎在FileConverter方法尚未完成调用之前,消息对话框窗口弹出窗口的速度就太快了。我想知道JOptionPane的放置是否正确,或者是否可以将消息延迟到该方法完成处理之前? 问题答案: 您可以使用SwingWorker。 在这里看看Java教程。

  • 问题内容: 我在延迟游戏中电脑的移动时遇到问题。 我找到了一些解决方案,但在我的情况下它们不起作用,例如 我试图将此与功能一起使用,但也没有任何效果。 还有其他可能性吗? 问题答案: 迅捷3 使用GCD: 或使用计时器: 迅捷2 使用GCD: 或使用计时器: