2012-04-08 65 views
2

我有下面的代碼應該顯示數組中的位置的幾個標記,每個標記點擊會顯示一個信息窗口一切正常,除了21我能夠只顯示8個標記。谷歌地圖V3:只顯示一些標記

// declare Variables 
    var geocoder; 
    var map; 
    var tex;  
    var markersArray = []; 
    // pids array 21 addreses 
    var pids = [{ad:'251 Pantigo Road Hampton Bays NY 11946',pid:'9'}, 
    {ad:'Amagensett Quiogue NY 11978',pid:'10'}, 
    {ad:'789 Main Street Hampton Bays NY 11946',pid:'12'}, 
    {ad:'30 Abrahams Path Hampton Bays NY 11946',pid:'14'}, 
    {ad:'3 Winnebogue Ln Westhampton NY 11977',pid:'15'}, 
    {ad:'44 White Oak Lane Montauk NY 11954',pid:'16'}, 
    {ad:'107 stoney hill road Bridgehampton NY 11932',pid:'17'}, 
    {ad:'250 Pantigo Rd Hampton Bays NY 11946',pid:'19'}, 
    {ad:'250 Pantigo Rd Hampton Bays NY 11946',pid:'20'}, 
    {ad:'44 Woodruff Lane Wainscott NY 11975',pid:'21'}, 
    {ad:'Address East Hampton NY 11937',pid:'46'}, 
    {ad:'Address Amagansett NY 11930',pid:'49'}, 
    {ad:'Address Remsenburg NY 11960 ',pid:'50'}, 
    {ad:'Address Westhampton NY 11977',pid:'51'}, 
    {ad:'prop address Westhampton Dunes NY 11978',pid:'52'}, 
    {ad:'prop address East Hampton NY 11937',pid:'53'}, 
    {ad:'Address East Hampton NY 11937',pid:'58'}, 
    {ad:'Address Southampton NY 11968',pid:'59'}, 
    {ad:'Address Bridgehampton NY 11932',pid:'60'}, 
    {ad:'Address Sagaponack NY 11962',pid:'61'}]; 

    // create an MVCobject for creating Info window on marker 
    var pin = new google.maps.MVCObject(); 
    // The content placeholder for the Info window. 
    var content = document.createElement("DIV"); 
    var title = document.createElement("DIV"); 
    content.appendChild(title); 
    // that is where you have the ajax result placed 
    var streetview = document.createElement("DIV"); 
    streetview.style.width = "326px"; 
    streetview.style.height = "212px"; 
    content.appendChild(streetview); 
    var infowindow = new google.maps.InfoWindow({ 
     content: content 
    }); 
     // Initialize 
    function initialize() { 
     geocoder = new google.maps.Geocoder(); 
     var latlng = new google.maps.LatLng(40.8687097, -73.0014946); 
     var myOptions = { 
      zoom: 8, 
      center: latlng, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     } 
     map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 
     getAllPids(); 
    } 
    // loop to create all markers 
    function getAllPids() { 
     var i; 
     for (i = 0; i < pids.length; i++) { 
      var test = pids[i]; 
      codeAddress(test); 
     }   
    } 
    // get latlng for each address , create marker and add eventlistner to click open infowindow 
    function codeAddress(place) { 
     geocoder.geocode({ 'address': place.ad }, function(results, status) { 
      if (status == google.maps.GeocoderStatus.OK) { 
       var marker = new google.maps.Marker({ 
        map: map, 
        position: results[0].geometry.location, 
        title: place.pid 
       }); 
       markersArray.push(marker);     
       google.maps.event.addListener(marker, "click", function() { 
        openInfoWindow(marker); 
       }); 
      } else { 
       // alert("Geocode was not successful for the following reason: " + status); 
      } 
     }); 
    } 
    // click event on marker calls this to show infowindow. 
    function openInfoWindow(marker) {   
    getContent(marker.getTitle());   
     pin.set("position", marker.getPosition()); 
     infowindow.open(map, marker); 
    } 
    // Now ajax call to get the content for the current info window 
    function getContent(pid) {   
     $.ajax({ 
         type: "POST", 
         url: "mapSearchResult.aspx/get_map",        
         data: "{'pids':'"+pid +"'}", 
         contentType: "application/json; charset=utf-8", 
         dataType: "json", 
         success: function(msg) { 

         title.innerHTML =msg.d; 

         } 
        });   
    } 
+0

您的代碼是否有有效的版本遷移到JSfiddle? – Hugolpz 2013-02-25 21:06:23

回答

9

如果註釋掉此

// alert("Geocode was not successful for the following reason: " + status); 

,你會發現reason codeOVER_QUERY_LIMIT(620)。

有一個比率限制:如果您在不引入延遲的情況下對多個地址進行地理編碼,則會打破該限制,因爲您在太短的時間內執行的操作太多。

您的某些地址不會進行地理編碼(或不會生成您想要的結果),因爲地址解析器使用郵政地址。地址如地址East Hampton NY 11937地址Westhampton Dunes NY 11978不是郵寄地址。

建議的策略不是浪費Google與其他人分享的資源。在一次性操作中對自己的地址進行地理編碼,將這些位置存儲在數據庫中(或直接在代碼中使用它們),然後使用座標定位標記。每次加載頁面時,不要打擾查找您已知的位置。只能使用地理編碼器預先找到您不知道的位置:您的用戶輸入的地址。

如果您確實必須始終顛覆該策略並始終對所有內容進行地理編碼,那麼您需要減慢請求速度。在您提交更多請求時,您可能不得不減慢速度以滿足地理編碼器的要求。我已經在http://acleach.me.uk/gmaps/v3/plotaddresses.htm —上做了一個版本3的例子(來自一個着名的版本2的例子),你可以看到它開始於請求之間100ms的延遲,但是在二十次迭代之後需要減速到約150ms。