添加时间延迟到谷歌地图地理编码

时间:2011-10-05 09:27:16

标签: google-maps google-maps-api-3 google-maps-markers

我正在研究这张地图并尝试对150个标记进行地理编码,但我正在达到地理编码限制。如何添加时间延迟以避免达到限制?

1 个答案:

答案 0 :(得分:2)

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

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

溶液。上述程序可以调整。

(1)时间间隔可以减少:

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

(2)函数geocode()可以在每个时间间隔执行多个请求,例如: 5个要求:

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