【问题标题】:How to setTimeout to provide over_query_limit如何设置超时以提供 over_query_limit
【发布时间】:2020-05-12 21:59:32
【问题描述】:

几天来,我一直在“苦苦挣扎”,代码 sn-p 应该输出具有多个地址的谷歌地图(带有 API 密钥的 v3)。

直到我没有超过“12”的数量,这一切都很好。现在我有 12 个地址,第 12 个地址出现“over_query_limit”错误。

在这篇帖子被立即屏蔽或删除之前,我已经阅读了所有现有的帖子,没有一个答案适合我的问题。

由于我不是真正“适合” JS,如果你们中的一个人能帮助我,那就太好了。

在我的 map.js 中迭代一个看起来像这样的数组

      $addresses[] = [$item->userCompany, 
                    $item->userStrasse.", ".
                    $item->userPlz." ".$item->userOrt, 
                    "eintrag/".$item->userEntryUrl,
                    $iconMarker
                    ];  
   <script>  
        var locations = <?php echo json_encode($addresses, JSON_PRETTY_PRINT); ?>;
    </script>

这就是我对这段代码所做的:

    function initialize() {
    map = new google.maps.Map(
    document.getElementById("map"), {
        center: new google.maps.LatLng(10.9027636, 49.8988135),
        maxZoom: 18,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });
    geocoder = new google.maps.Geocoder();

    for (i = 0; i < locations.length; i++) {
            geocodeAddress(locations, i);
    }
}

google.maps.event.addDomListener(window, "load", initialize);

function geocodeAddress(locations, i) {
    var title = locations[i][0];
    var address = locations[i][1];
    var url = locations[i][2];
    var markerImage = locations[i][3];
    **geocoder.geocode({
        'address': locations[i][1]
    },**

    function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var marker = new google.maps.Marker({
                //icon: 'https://maps.google.com/mapfiles/ms/icons/blue.png',
                icon: markerImage,
                map: map,
                position: results[0].geometry.location,
                title: title,
                animation: google.maps.Animation.DROP,
                address: address,
                url: url
            })
            infoWindow(marker, map, title, address, url);
            bounds.extend(marker.getPosition());
            map.fitBounds(bounds);
        } else {
            //alert("geocode of " + address + " failed:" + status);
        }
    });
}

如何在此处添加“setTimeout”,以避免出现“over_query_limit”错误?

【问题讨论】:

    标签: jquery settimeout


    【解决方案1】:

    我认为超时的问题在于它会延迟每个循环,但是是异步的。因此,在延迟结束时,它们最终都会叠加在一起。

    但是,如果我们每次通过乘以 i 来增加 setTimeout() 的长度,它可能会起作用。

    for (i = 0; i < locations.length; i++) {
            setTimeout(geocodeAddress(locations, i), 1000 * i);            
    }
    

    现在如果这不起作用,试试这个自定义的睡眠功能,它也可能起作用。

    function sleep(milliSeconds) {
      var startTime = new Date().getTime();
      while (new Date().getTime() < startTime + milliSeconds);
    }
    for (i = 0; i < locations.length; i++) {
            geocodeAddress(locations, i);
            sleep(2200);
    }
    

    【讨论】:

    • 嗨,谢谢,我已经试过了,没有任何效果。尽管如此,错误还是出现了。还有其他想法吗?
    • 非常感谢您的支持。第一个也没有效果,即使我将超时时间增加到 4500,地图也会立即出现,但在第 11 个条目后也会停止。这意味着,未显示第 12 个入口。第二个已经在编辑器上显示错误。无法尝试这个。也许某些“komma”缺少某些东西。
    • @itsberni 你现在可以试试吗?
    • 对不起 - 不。这是实施此超时的正确位置吗?
    猜你喜欢
    • 2013-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-04
    • 1970-01-01
    • 2019-06-25
    • 2011-08-09
    相关资源
    最近更新 更多