2012-03-11 123 views
8

我有一個非常奇怪的錯誤,當我嘗試調整地圖大小以適應放置在其上的標記時,導致瀏覽器凍結。map.fitBounds(bounds)導致瀏覽器凍結

參考我的大小調整代碼:

function sizeMap() { 
      // create a new bounds object to define the new boundry 
      var bounds = new google.maps.LatLngBounds(); 
      // loop through each the string latlang values held in the latlng 
      // aray 
      for (var i = 0; i <latlngArray.length ; i++) { 
       // create a new LatLng object based on the string value 
       var tempLatLng = new google.maps.LatLng(latlngArray[i]); 
       // extend the latlng bounds based on the new location 
       bounds.extend(tempLatLng); 
      } 
      // map.fitBounds(bounds); <== everything seems to work fine until this line is uncommented 
     } 

問題是我不能正確地調試它使用Firebug,因爲凍結到了! 任何人對這個問題可能有什麼想法?

在此先感謝。

更新:

在回答puckheads質疑下面的代碼顯示了經緯度陣列填充入手:

function geocodeAddress (address) { 

      var geocoder = new google.maps.Geocoder(); 
      geocoder.geocode({'address': address}, function(results, status) { 
       if (status == google.maps.GeocoderStatus.OK) { 
        loadMarker(results[0].geometry.location,"Info here",address); 
        latlngArray.push(results[0].geometry.location); <== latlngs are first added here they are as a result of a call to the google geocode service with a standard string address. 
       } else { 
        alert("Geocode was not successful for the following reason: " +" "+ status); 
       } 
      }); 
     } 
+1

您是否解決了這個問題? - 如果是的話,你應該接受或寫出你自己的正確答案! – levi 2014-07-08 15:43:03

+0

您是否嘗試過使用Chrome和JavaScript控制檯? – MrUpsidown 2015-04-21 11:37:00

回答

-1

你不從字符串創建一個新的經緯度。它由2個數字創建,儘管它看起來2個字符串也可以工作,一個用於緯度,另一個用於經度。一個字符串不起作用。

因此可以這樣說new google.maps.LatLng(-25.363882, 131.044922)甚至new google.maps.LatLng('-25.363882', '131.044922')但不new google.maps.LatLng('-25.363882, 131.044922') 所以你需要通過2個獨立的價值新google.maps.LatLng但似乎你只傳遞一個。

http://code.google.com/apis/maps/documentation/javascript/reference.html#LatLng

+0

我已經嘗試了Marks的建議,結果是一樣的,我也嘗試使用parseFloat()將兩個結果字符串轉換爲浮點數,並且仍然沒有快樂。 – user1079178 2012-03-11 17:34:45

+1

你能舉一個latLngArray中的內容的例子嗎? – puckhead 2012-03-11 18:21:35

0

據我所知,你不能創建從一個單一字符串參數的LatLng對象。如果你有一個形式爲「lat,lng」的字符串,那麼在創建LatLng對象之前將它們分開。循環內部看起來像這樣:

 var tempLatLng = latlngArray[i].split(','); 
     bounds.extend(new google.maps.LatLng(tempLatLng[0], tempLatLng[1])); 
+0

他的數組已經包含LatLng對象。不是字符串。 – MrUpsidown 2015-04-21 11:34:05

-1

根據您更新的代碼,您根本不需要創建tempLatLng。你的數組中已經包含經緯度的對象,所以你應該可以說,在for循環bounds.extend(latlngArray[i]) ...

+0

嘗試了你的建議,並沒有設置焦點,但它已經停止凍結感謝puckhead,至少我現在可以調試它。 – user1079178 2012-03-13 18:10:54

+0

你的焦點沒有被設置爲什麼?問題是否得到解決? – puckhead 2012-03-14 00:03:57

+0

對不起,可能沒有解釋我的自我,我的意思是瀏覽器不再凍結,但視圖端口沒有調整到標記的邊界,因爲它應該。 – user1079178 2012-03-14 15:17:54

-1

使用定時器:

var Timer=""; 
    $(window).resize(function() { 
     clearInterval(Timer); 
     run_timer(); 
    }); 
    function run_timer() { 
     Timer = setInterval(function(){sizeMap();}, 1500); 
    } 
    function sizeMap() { 
     clearInterval(Timer); 
     // create a new bounds object to define the new boundry 
     var bounds = new google.maps.LatLngBounds(); 
     // loop through each the string latlang values held in the latlng 
     // aray 
     for (var i = 0; i <latlngArray.length ; i++) { 
      // create a new LatLng object based on the string value 
      var tempLatLng = new google.maps.LatLng(latlngArray[i]); 
      // extend the latlng bounds based on the new location 
      bounds.extend(tempLatLng); 
     } 
     // map.fitBounds(bounds); <== everything seems to work fine until this line is uncommented 
    } 
4

的問題是其他人所說的,你嘗試創建一個經緯度以另一個LatLng對象作爲參數的對象。它只接受2或3個參數,其中前兩個是數字。 https://developers.google.com/maps/documentation/javascript/reference#LatLng

但是,您完全可以跳過創建新LatLng對象的部分,並使用循環內數組中的當前一個。

這解決了這個問題:http://jsfiddle.net/y9ze8a1f/1/

function sizeMap() { 
    // create a new bounds object to define the new boundry 
    var bounds = new google.maps.LatLngBounds(); 
    // loop through each the string latlang values held in the latlng 
    // aray 
    for (var i = 0; i <latlngArray.length ; i++) { 
     // This is causing the issue, you can only create a LatLng with two separate latitude and longitude arguments 
     //var tempLatLng = new google.maps.LatLng(latlngArray[i]); 

     bounds.extend(latlngArray[i]); 
    } 
    map.fitBounds(bounds); 
} 

如果您想基於舊創建一個新的經緯度,雖然,你可以做到這一點通過經緯度方法lat()lng()

http://jsfiddle.net/q7sh4put/

function sizeMap() { 
    // create a new bounds object to define the new boundry 
    var bounds = new google.maps.LatLngBounds(); 
    // loop through each the string latlang values held in the latlng 
    // aray 
    for (var i = 0; i <latlngArray.length ; i++) { 
     // create a new LatLng object based on the string value 
     var tempLatLng = new google.maps.LatLng(latlngArray[i].lat(), latlngArray[i].lng()); 

     // extend the latlng bounds based on the new location 
     bounds.extend(tempLatLng); 
    } 
    map.fitBounds(bounds); 
} 
+0

但這裏主要的問題是「它導致瀏覽器凍結」。這是因爲腳本在週期中長時間運行......我用大膽的方式標記了它,以便更好地看到它。 – Legionar 2015-04-22 12:41:19

+1

我知道了,它被凍結的原因是最大的callstack達到了多次,這是通過錯誤的參數實例化'google.maps.LatLng'而來的。因此,實際發生的代碼是在Google的代碼中,但是由代碼調用引起的。 (您的原始代碼在哪裏達到了callstack:http://jsfiddle.net/y9ze8a1f/2/) – thebreiflabb 2015-04-22 16:17:46

+0

好的,謝謝... – Legionar 2015-04-23 07:13:55