2012-03-11 124 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