未捕获的RangeError:当我尝试设置范围时,最大调用堆栈大小超出了Google地图


72

我创建了一个有效的Google地图,现在尝试自动缩小以使所有点都适合。

当我添加时:

var bounds = new google.maps.LatLngBounds();

bounds.extend(point);
map.fitBounds(bounds); 

然后我得到这个错误:

Uncaught RangeError: Maximum call stack size exceeded
J.get
(anonymous function)
Ey.(anonymous function).zoomRange_changed
Zf
J.set
(anonymous function)
Ey.(anonymous function).zoomRange_changed
Zf
J.set
(anonymous function)
Ey.(anonymous function).zoomRange_changed
Zf
J.set
(anonymous function)
Ey.(anonymous function).zoomRange_changed
Zf
J.set
(anonymous function)
Ey.(anonymous function).zoomRange_changed
Zf
J.set
(anonymous function)
Ey.(anonymous function).zoomRange_changed
Zf
J.set
(anonymous function)
Ey.(anonymous function).zoomRange_changed
Zf
J.set
(anonymous function)
Ey.(anonymous function).zoomRange_changed
Zf
J.set
(anonymous function)
Ey.(anonymous function).zoomRange_changed
Zf
J.set
(anonymous function)
Ey.(anonymous function).zoomRange_changed
Zf
J.set
(anonymous function)
Ey.(anonymous function).zoomRange_changed
Zf
J.set
(anonymous function)
Ey.(anonymous function).zoomRange_changed
Zf
J.set
(anonymous function)
Ey.(anonymous function).zoomRange_changed
Zf
J.set
(anonymous function)
Ey.(anonymous function).zoomRange_changed
Zf
J.set
(anonymous function)
Ey.(anonymous function).zoomRange_changed
Zf
J.set
(anonymous function)
Ey.(anonymous function).zoomRange_changed
Zf
J.set
(anonymous function)
Ey.(anonymous function).zoomRange_changed
Zf
J.set
(anonymous function)
Ey.(anonymous function).zoomRange_changed
Zf
J.set
(anonymous function)
Ey.(anonymous function).zoomRange_changed
Zf
J.set
(anonymous function)
Ey.(anonymous function).zoomRange_changed
Zf
J.set
(anonymous function)
Ey.(anonymous function).zoomRange_changed
Zf
J.set
(anonymous function)
Ey.(anonymous function).zoomRange_changed
Zf
J.set
(anonymous function)
Ey.(anonymous function).zoomRange_changed
Zf
J.set
(anonymous function)
Ey.(anonymous function).zoomRange_changed
Zf
J.set
(anonymous function)
Ey.(anonymous function).zoomRange_changed
Zf
J.set
(anonymous function)
Ey.(anonymous function).zoomRange_changed
Zf
J.set
(anonymous function)
Ey.(anonymous function).zoomRange_changed
Zf
J.set
(anonymous function)
Ey.(anonymous function).zoomRange_changed
Zf
J.set
(anonymous function)
Ey.(anonymous function).zoomRange_changed
Zf
J.set
(anonymous function)
Ey.(anonymous function).zoomRange_changed
Zf
J.set
(anonymous function)
Ey.(anonymous function).zoomRange_changed
Zf
J.set
(anonymous function)
Ey.(anonymous function).zoomRange_changed
Zf
J.set
(anonymous function)
Ey.(anonymous function).zoomRange_changed
Zf
J.set
(anonymous function)
Ey.(anonymous function).zoomRange_changed
Zf
J.set
(anonymous function)
Ey.(anonymous function).zoomRange_changed
Zf
J.set
(anonymous function)
Ey.(anonymous function).zoomRange_changed
Zf
J.set
(anonymous function)
Ey.(anonymous function).zoomRange_changed
Zf
J.set
(anonymous function)
Ey.(anonymous function).zoomRange_changed
Zf
J.set
(anonymous function)
Ey.(anonymous function).zoomRange_changed
Zf
J.set
(anonymous function)
Ey.(anonymous function).zoomRange_changed
Zf
J.set
(anonymous function)
Ey.(anonymous function).zoomRange_changed
Zf
J.set
(anonymous function)
Ey.(anonymous function).zoomRange_changed
Zf
J.set
(anonymous function)
Ey.(anonymous function).zoomRange_changed
Zf
J.set
(anonymous function)
Ey.(anonymous function).zoomRange_changed
Zf
J.set
(anonymous function)
Ey.(anonymous function).zoomRange_changed
Zf
J.set
(anonymous function)
Ey.(anonymous function).zoomRange_changed
Zf
J.set
(anonymous function)
Ey.(anonymous function).zoomRange_changed
Zf
J.set
(anonymous function)
Ey.(anonymous function).zoomRange_changed
Zf
J.set
(anonymous function)
Ey.(anonymous function).zoomRange_changed
Zf
J.set
(anonymous function)
Ey.(anonymous function).zoomRange_changed
Zf

我创建地图的所有代码是:

function load() {
      var map = new google.maps.Map(document.getElementById("map"), {
        center: new google.maps.LatLng(47.6145, -122.3418),
        zoom: 13,
        mapTypeId: 'roadmap'
      });
      var infoWindow = new google.maps.InfoWindow;

      // Change this depending on the name of your PHP file
      downloadUrl("getMapMarkers.php", function(data) {
        var bounds = new google.maps.LatLngBounds();
        var xml = data.responseXML;
        var markers = xml.documentElement.getElementsByTagName("marker");
        for (var i = 0; i < markers.length; i++) {
          var name = markers[i].getAttribute("name");

          var type = markers[i].getAttribute("type");
          var point = new google.maps.LatLng(
              parseFloat(markers[i].getAttribute("lat")),
              parseFloat(markers[i].getAttribute("lng")));
          var html = "<b>" + name + "</b> <br/>" ;
          var icon = customIcons[type] || {};
          var marker = new google.maps.Marker({
            map: map,
            position: point,
            icon: icon.icon,
            shadow: icon.shadow
          });
          bindInfoWindow(marker, map, infoWindow, html);
          bounds.extend(point);
          map.fitBounds(bounds);
        }
      });


    }

3
map.fitBounds(bounds);你的循环的了,你只需要做到这一次,所有的循环之后。但是不确定是否会导致此错误
Duncan 2013年

我有同样的错误,只是在茉莉花规格上。规格通过并且实际视图按预期工作。
Giorgio Bozio

Answers:


145

我碰到了同样的异常,事实证明传递给“ new google.maps.LatLng”的经度是不确定的。


25
如果传递的值是字符串(或数字以外的其他值),也会发生这种情况。
HalilÖzgür2013年

为新的google.maps.LatLng传递字符串时,我遇到了同样的问题。值得注意的是,这不是在FF中发生,仅在Chrome中发生。
Paflow

这也是我的问题。我当时使用WIPMANIA API来获取经度/纬度,但是由于我以与Ajax加载的内容异步加载了Google Map api,因此在初始化后以某种方式我犯了加载WIP的错误。
balslev

3
尽管这篇文章暗示了正确的方向,但这不是答案,而是评论。因此,当的至少一个参数new google.maps.LatLng(...)undefined或时,我们会收到该错误NaN
苦蓝

4

可能的情况

  • 或者map.fitBounds(bounds);不接收纬度/经度或两者。
  • 接收到的纬度/经度值格式不正确(NaN)。

就我而言,我正在使用弹性搜索和bounds.push(latlng);。取错误的值。就我而言,我拼错了经纬度。ES返回的是lat和lon,而我使用的是lat和lng。
阿威斯·穆斯塔法

2

我使用了谷歌地图插件:http : //code.google.com/p/jquery-ui-map/ 我发现自己也遇到了同样的问题,正如Ollie所说,我的json格式错误。这是我的json

mapObj= 
[
      {
         "latitude": 57.797333,
         "longitude": 12.050211,
         "title": "Angered",
         "content": "Representing :)"
      },
      {
         "latitude": 57.696995,
         "longitude": "11.9865",
         "title": "Gothenburg",
         "content": "Swedens second largest city"
      }
 ]

由于经度是用字符串写的,所以我遇到了这个错误。

求助:所有坐标都为数字。

添加地图(假设本地json,如果未参考引用链接):

    $('#map_canvas').gmap().bind('init', function() { 
    $.each( mapObj, function(i, marker) {
        $('#map_canvas').gmap('addMarker', { 
            'position': new google.maps.LatLng(marker.latitude, marker.longitude), 
            'bounds': true 
        }).click(function() {
            $('#map_canvas').gmap('openInfoWindow', { 'content': marker.content }, this);
        });
    });
});


1

设置NaN为的值时,似乎也会引发此错误zoom

map.setZoom(null)      // => nothing happens
map.setZoom(undefined) // => nothing happens 
map.setZoom(NaN)       // => error!
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.