首页 > web前端 > js教程 > 如何防止 v3 中的 Google Maps API OVER_QUERY_LIMIT 错误?

如何防止 v3 中的 Google Maps API OVER_QUERY_LIMIT 错误?

Patricia Arquette
发布: 2024-11-02 22:50:02
原创
414 人浏览过

How to Prevent the Google Maps API OVER_QUERY_LIMIT Error in v3?

如何处理 Google Maps API v3 中的 OVER_QUERY_LIMIT 错误

简介:

在 Google Maps API v3 中,您可以短时间内发出过多地理编码请求时遇到 OVER_QUERY_LIMIT 错误。为了避免此错误,需要在请求之间实现延迟机制。

JavaScript 实现:

要在地理编码调用之间引入暂停,您可以使用以下 JavaScript代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

<code class="javascript">function codeAddress(vPostCode) {

  if (geocoder) {

    // If there's an existing delay, wait for it to finish

    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

        });

      } else if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {

        wait = true;

        setTimeout(function() {

          wait = false;

        }, 2000); // Set a delay of 2000 milliseconds

      } else {

        alert("Geocode was not successful for the following reason: " + status);

      }

    });

  }

}</code>

登录后复制

此代码检查现有延迟,如果发生 OVER_QUERY_LIMIT 错误,则设置新的延迟,并等待延迟完成后再恢复地理编码。

示例:

在您提供的代码中,您可以将现有的 codeAddress 函数替换为更新版本:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

<code class="javascript">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

        });

      } else if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {

        wait = true;

        setTimeout(function() {

          wait = false;

        }, 2000);

      } else {

        alert("Geocode was not successful for the following reason: " + status);

      }

    });

  }

}</code>

登录后复制

此修改将引入必要的延迟以防止 OVER_QUERY_LIMIT 错误。

以上是如何防止 v3 中的 Google Maps API OVER_QUERY_LIMIT 错误?的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板