javascript 未捕获的 RangeError:当我使用谷歌地图时超出了最大调用堆栈大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31866535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 14:29:20  来源:igfitidea点击:

Uncaught RangeError: Maximum call stack size exceeded when I use google maps

javascriptgoogle-maps-api-3stack-overflow

提问by gstackoverflow

I am trying to write simple example with google maps and markers but i faces with error:

我正在尝试使用谷歌地图和标记编写简单的示例,但我遇到了错误:

Uncaught RangeError: Maximum call stack size exceeded

js:

js:

$(document).ready(function() {
    var map;
    var elevator;
    var myOptions = {
        zoom: 1,
        center: new google.maps.LatLng(0, 0),
        mapTypeId: 'terrain'
    };
    map = new google.maps.Map($('#map_canvas')[0], myOptions);

    var markers = [];
    coords = [{
        lat: 55,
        lng: 37
    }];
    for (var x = 0; x < coords.length; x++) {
        var latlng = new google.maps.LatLng(x.lat, x.lng);
        var marker = new google.maps.Marker({
            position: latlng,
            map: map
        });
        markers.push(marker);


    }

    fitBounds();

    function fitBounds() {
        var bounds = calculateBounds();
        if (bounds != undefined) {
            map.fitBounds(bounds);
        }
    }

    function calculateBounds() {
        var allMarkers = markers;
        if (allMarkers.length > 0) {
            var bounds = new google.maps.LatLngBounds();
            for (i = 0; i < allMarkers.length; i++) {
                bounds.extend(allMarkers[i].getPosition());
            }
        }
        return bounds;
    }

});

fiddle

小提琴

回答by geocodezip

The error Uncaught RangeError: Maximum call stack size exceededwhen calling fitBoundsusually means you have called bounds.extendwith an invalid google.maps.LatLngobject.

Uncaught RangeError: Maximum call stack size exceeded调用时的错误fitBounds通常意味着您调用bounds.extend了一个无效的google.maps.LatLng对象。

This is incorrect (x.lat and x.lng are undefined):

这是不正确的(x.lat 和 x.lng 未定义):

for (var x = 0; x < coords.length; x++) {
    var latlng = new google.maps.LatLng(x.lat, x.lng);
    var marker = new google.maps.Marker({
        position: latlng,
        map: map
    });
    markers.push(marker);
}

It yields latlng with lat: NaN, lng: NaN

它产生 latlng 与 lat: NaN, lng: NaN

Which isn't valid.

这是无效的。

should be:

应该:

for (var x = 0; x < coords.length; x++) {
    var latlng = new google.maps.LatLng(coords[x].lat, coords[x].lng);
    var marker = new google.maps.Marker({
        position: latlng,
        map: map
    });
    markers.push(marker);
}

updated fiddle

更新的小提琴