添加时间延迟谷歌地图地理编码 [英] Adding time delay to google map geocode

查看:122
本文介绍了添加时间延迟谷歌地图地理编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理这张地图,并尝试对大约150个标记进行地理编码,但是我正在使用地理编码限制。如何添加时间延迟以避免触及限制?

解决方案

这为地理编码添加了一个计时器,因此每个标记都有一个延迟。

  //为每个城市添加一个LatLng对象
函数geocodeAddress(i){
geocoder .geocode({'address':address [i]},function(results,status){
if(status == google.maps.GeocoderStatus.OK){
places [i] = results [ 0] .geometry.location;

//添加标记
var marker = new google.maps.Marker({position:places [i],map:map});
markers.push(marker);
mc.addMarker(marker);

//创建事件侦听器,它现在可以访问i和marker的值它的创建
google.maps.event.addListener(marker,'click',function(){
//检查我们是否已经有InfoWindow
if(! infowindow){
infowindow = new google.maps.InfoWindow();
}

//设置InfoWindow的内容
infowindow.setContent(popup_content [i]);

//将InfoWindow绑定到标记
infowindow.open(map,marker);
});

//用每个LatLng
bounds.extend(places [i])扩展边界对象;

//将地图调整到新的边界框
map.fitBounds(bounds)
} else {
alert(地理编码不成功,原因如下: +状态); (){
}
})
}

函数geocode(){
if(geoIndex< address.length){
geocodeAddress(geoIndex );
++ geoIndex;
}
else {
clearInterval(geoTimer);
}
}
var geoIndex = 0;
var geoTimer = setInterval(geocode,200); // 200毫秒(试用)

var markerCluster = new MarkerClusterer(map,markers);
}
})
();
< / script>

ADDED。上述程序可以调整。



(1)时间间隔可以缩短:

  var geoTimer = setInterval(geocode,100); (2)函数geocode()可以在每个请求上执行多个请求时间间隔,例如5个请求:

 函数geocode(){
for(var k = 0; k< 5& amp ; geoIndex< address.length; ++ k){
geocodeAddress(geoIndex);
++ geoIndex;
}
if(geoIndex> = address.length){
clearInterval(geoTimer);
}
}


I'm working on this map and trying to geocode around 150 markers but I'm hitting the geocode limit. How can I add a time delay to avoid hitting the limit?

解决方案

This adds a timer to the geocoding so each marker has a delay.

// Adding a LatLng object for each city 
function geocodeAddress(i) {
     geocoder.geocode( {'address': address[i]}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            places[i] = results[0].geometry.location;

            // Adding the markers 
            var marker = new google.maps.Marker({position: places[i], map: map});
            markers.push(marker);
            mc.addMarker(marker);

            // Creating the event listener. It now has access to the values of i and marker as they were during its creation
            google.maps.event.addListener(marker, 'click', function() {
                // Check to see if we already have an InfoWindow
                if (!infowindow) {
                    infowindow = new google.maps.InfoWindow();
                }

                // Setting the content of the InfoWindow
                infowindow.setContent(popup_content[i]);

                // Tying the InfoWindow to the marker 
                infowindow.open(map, marker);
            });

            // Extending the bounds object with each LatLng 
            bounds.extend(places[i]); 

            // Adjusting the map to new bounding box 
            map.fitBounds(bounds) 
        } else { 
            alert("Geocode was not successful for the following reason: " + status); 
        }
    })
}

function geocode() {
    if (geoIndex < address.length) {
        geocodeAddress(geoIndex);
        ++geoIndex;
    }
    else {
        clearInterval(geoTimer);
    }
}
var geoIndex = 0;
var geoTimer = setInterval(geocode, 200);  // 200 milliseconds (to try out)

var markerCluster = new MarkerClusterer(map, markers); 
} 
})
(); 
</script> 

ADDED. The above program can be tuned.

(1) The time interval can be reduced:

var geoTimer = setInterval(geocode, 100);  // do requests each 100 milliseconds 

(2) The function geocode() could perform several requests at each time interval, e.g. 5 requests:

function geocode() {
    for (var k = 0; k < 5 && geoIndex < address.length; ++k) {
        geocodeAddress(geoIndex);
        ++geoIndex;
    }
    if (geoIndex >= address.length) {
        clearInterval(geoTimer);
    }
}

这篇关于添加时间延迟谷歌地图地理编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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