2011-10-05 65 views
1

我正在處理此地圖並試圖對150個標記進行地理編碼,但是我正在點擊地理編碼限制。如何添加時間延遲以避免達到上限?將時間延遲添加到Google地圖地理編碼

+0

如果您只提供不帶PHP的Javascript代碼,尤其是當您希望將答案合併到您的代碼中時,將會有所幫助。 – Jiri

+0

@Jiri通過查看提供的鏈接的來源可以很容易地找到它。由於其他方面與之前的php發生衝突,我已經將其包含在內。 – Rob

回答

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> 

ADDED。上述程序可以調整。

(1)可減少的時間間隔:

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

(2)函數的地址解析()可以在每個時間間隔執行若干請求,例如5請求:

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

感謝您的回覆。我會放棄這一點,我用Javascript來垃圾,所以我可能會搞砸了! – Rob

+0

你可能把它放到我的代碼中嗎?從「//爲每個城市添加一個LatLng對象」行開始,直到關閉?我只是嘗試和搞砸了,我的JavaScript是可怕的! – Rob

+0

我編輯了我的代碼,以便將它從「爲每個城市添加LatLng對象」複製到文件末尾。祝你好運! – Jiri