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
Uncaught RangeError: Maximum call stack size exceeded when I use google maps
提问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;
}
});
回答by geocodezip
The error Uncaught RangeError: Maximum call stack size exceeded
when calling fitBounds
usually means you have called bounds.extend
with an invalid google.maps.LatLng
object.
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);
}