最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Uncaught RangeError: Maximum call stack size exceeded when I use google maps - Stack Overflow

matteradmin3PV0评论

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:

$(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

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:

$(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

Share Improve this question edited Aug 6, 2015 at 22:09 geocodezip 161k14 gold badges226 silver badges255 bronze badges asked Aug 6, 2015 at 21:59 gstackoverflowgstackoverflow 36.6k138 gold badges418 silver badges784 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 9

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.

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

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

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

code snippet:

$(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(coords[x].lat, coords[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;
  }

});
html,
body {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px;
}

#map_canvas {
  height: 100%;
  width: 100%;
}
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maps.googleapis./maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>

<div id="map_canvas"></div>

Post a comment

comment list (0)

  1. No comments so far