本文介绍了超过setTimeout的查询限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Google地图地理编码器并越过查询限制。
这是我的代码

I'm trying to use google maps geocoder and getting over query limit.this is my code

var geocoder;
var map;
function initialize() {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(-34.397, 150.644);
    var mapOptions = {
        zoom: 12,
        center: latlng
    }
    map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
}
function codeAddress() {
    for (var i = 1; i < 20; i++) {
        var address = document.getElementById('address' + " " + i).value;
        setTimeout(geocoder.geocode({ 'address': address }, 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);
            }
        }));
    }
}
google.maps.event.addDomListener(window, 'load', initialize, codeAddress.bind);


推荐答案

您没有指定延迟 setTimeout

您必须将其设置为递增值以避免错误,例如 i * 150 ,那么电话将会在150ms,300ms,450ms等等之后。

You must set it to an incrementing value to avoid the error, e.g. i*150 , then the calls will be after 150ms,300ms,450ms and so on

function codeAddress() {
    for (var i = 1; i < 20; i++) {
        var address = document.getElementById('address' + " " + i).value;
        setTimeout(geocoder.geocode({ 'address': address }, 
          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);
            }
        }),
        i*150//<-delay for the timeout   in ms
        );

    }
}

这篇关于超过setTimeout的查询限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 14:00