2013-12-19 125 views
1

我有一個問題。 。我怎樣才能防止這個無限循環,因爲即時通訊新的谷歌地圖和JQuery。防止無限循環

香港專業教育學院讀答案的很多,但連一個也沒有工作

這是我的代碼:

<script type="text/javascript"> 
    var geocoder, infoBubble; 
    var map; 
    //var mgr; 

    function initialize() { 
     var minZoomLevel = 4; 
     var zooms = 7; 
     geocoder = new google.maps.Geocoder(); 

     map = new google.maps.Map(document.getElementById('map'), { 
      zoom: minZoomLevel, 
      center: new google.maps.LatLng(38.50, -90.50), 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     }); 

     // Bounds for North America 
     var strictBounds = new google.maps.LatLngBounds(
    new google.maps.LatLng(15.70, -160.50), 
    new google.maps.LatLng(68.85, -55.90) 
    ); 

     // Listen for the dragend event 
     google.maps.event.addListener(map, 'dragend', function() { 
      if (strictBounds.contains(map.getCenter())) return; 

      // We're out of bounds - Move the map back within the bounds 

      var c = map.getCenter(), 
     x = c.lng(), 
     y = c.lat(), 
     maxX = strictBounds.getNorthEast().lng(), 
     maxY = strictBounds.getNorthEast().lat(), 
     minX = strictBounds.getSouthWest().lng(), 
     minY = strictBounds.getSouthWest().lat(); 

      if (x < minX) x = minX; 
      if (x > maxX) x = maxX; 
      if (y < minY) y = minY; 
      if (y > maxY) y = maxY; 

      map.setCenter(new google.maps.LatLng(y, x)); 
     }); 


     // Limit the zoom level 
     google.maps.event.addListener(map, 'zoom_changed', function() { 
      if (map.getZoom() < minZoomLevel) map.setZoom(minZoomLevel); 
     }); 
     codeAddress(); 
    } 

    var iconBase = 'https://maps.google.com/mapfiles/kml/shapes/'; 
    function codeAddress() { 
     infoBubble = new InfoBubble({ 
      map: map, 
      shadowStyle: 0, 
      padding: 10, 
      borderRadius: 10, 
      arrowSize: 15, 
      maxWidth: 300, 
      borderWidth: 1, 
      borderColor: '#ccc', 
      arrowPosition: 50, 
      arrowStyle: 0 
     }); 
     $.getJSON('/Dashboard/LoadWorkerList', function (address) { 
      $.each(address, function() { 
       var currVal = this["AddressLine1"]; 
       var Name = this["Name"]; 
       var Gender = this["Gender"]; 
       var Bdate = this["Birthdate"]; 
       var ID = this["Worker_ID"]; 
       geocoder.geocode({ 'address': currVal }, function (results, status) { 
        if (status == google.maps.GeocoderStatus.OK) { 
         var marker = new google.maps.Marker({ 
          map: map, 
          icon: iconBase + 'man.png', 
          position: results[0].geometry.location, 
          title: currVal 
         }) 

         var link = $('<a href="#">' + currVal + '</a>') 
         .data('location', results[0].geometry.location); 
         $('#places').append($('<li>').append(link)); 
         link.on('click', function (event) { 
          event.preventDefault(); 
          google.maps.event.trigger(address[0], "click"); 
          infoBubble.removeTab(0); 
          infoBubble.addTab(Name, "Name: " + Name + "<br> Address: " + currVal + "<br> Gender: " + Gender + "<br> Birthdate: " + Bdate + "<br><br>" + '<center><a href="/Worker/WorkerDetails?workerId=' + ID + '">View Profile</a></center>'); 
          infoBubble.open(map, marker); 
         } 
); 

         google.maps.event.addListener(map, 'idle', function() { 
          $('#places li a').css('display', function() { 
           return (map.getBounds().contains($(this).data('location'))) 
         ? '' 
         : 'none'; 
          }); 
         }); 



         google.maps.event.addListener(marker, 'click', (function (marker, i) { 
          return function() { 
           infoBubble.removeTab(0); 
           infoBubble.addTab(Name, "Name: " + Name + "<br> Address: " + currVal + "<br> Gender: " + Gender + "<br> Birthdate: " + Bdate + "<br><br>" + '<center><a href="/Worker/WorkerDetails?workerId=' + ID + '">View Profile</a></center>'); 
           infoBubble.open(map, marker); 
          } 
         })(marker, currVal)); 
         address.push(marker); 

        } 
        else if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT) { 
         setTimeout(codeAddress, 2000); 
        } 
        else { 
         alert("Geocode was not successful for the following reason: " + status); 
        } 
       }); 
      }); 
      google.maps.event.trigger(map, 'bounds_changed'); 
     }); 
    } 

    window.onload = function() { 
     initialize(); 
    } 

</script> 

我認爲這是什麼原因造成的項目環

` window.onload = function() { 
      initialize(); 
     }` 

和此

` google.maps.event.addListener(map, 'zoom_changed', function() { 
       if (map.getZoom() < minZoomLevel) map.setZoom(minZoomLevel); 
      }); 
      codeAddress(); <------- This one` 

回答

2

初始化的調用不是無限循環的原因,初始化將被調用1次。

我看到一個無限循環的唯一原因是codeAddress:

else if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT) { 
        setTimeout(codeAddress, 2000); 
       } 

當這種情況是真實的,你叫codeAddress又什麼都沒有區別,你會一次又一次地得到相同的結果(用延遲2秒)。

您可以:

  1. /Dashboard/LoadWorkerList回報最多隻有10個地址
  2. ommit在/Dashboard/LoadWorkerList地址的回報已經在先前的請求被退回

或:

使用增加超時爲geocoder.geocode(增加它150ms)的每個呼叫

+0

Dr.Molle是正確的:在json文件中有2個條目我得到的結果沒有任何問題。增加到12我得到了'OVER_QUERY_LIMIT'錯誤。 –

+0

先生。實際上可能會顯示的標記數量大約是10000個地址。 。我怎麼處理它,因爲它使我的地圖崩潰。 ..另一位先生。 .how我應該Ommit返回LoadWorkerList –

+0

先生,我有一個問題。 。爲什麼我的控制器已經被JsonResult訪問過,即使我只在登錄屏幕上? –