Добавление задержки в геокоде карты Google - PullRequest
1 голос
/ 05 октября 2011

Я работаю над этой картой и пытаюсь геокодировать около 150 маркеров, но я превышаю предел геокодирования.Как я могу добавить задержку, чтобы избежать превышения лимита?

1 Ответ

2 голосов
/ 05 октября 2011

Это добавляет таймер к геокодированию, чтобы у каждого маркера была задержка.

// 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);
    }
}
...