troubleshooting Question

Google Maps API - stack overflow at line 26

Avatar of saturation
saturation asked on
3 Comments1 Solution1423 ViewsLast Modified:
I am running the following code, and in the IE console I'm getting "stack overflow at line 26".  Anyone know what I may be doing wrong here?  I'm perplexed.

<HTML><HEAD><META http-equiv="Content-Type" content="text/html; charset=utf-8">

<!DOCTYPE html>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Google Maps JavaScript API v3 Example: Geocoding Simple</title>
    <style type="text/css">
      html { height: 100% }
        body { height: 100%; margin: 0; padding: 0 }
        #map-canvas { height: 100% }
      <script src="" type="text/javascript"></script>
    <script src="" type="text/javascript"></script>

                    var latlngs = [];
                    var startingPoint;
                    var locations = ['1850 n. damen, chicago, il','1050 w. division, chicago, il', 'w cortland st and n damen ave, chicago, il', 'wicker park, n damen ave, chicago, il'];

                        var geocoder = new google.maps.Geocoder();
                        geocoder.geocode({'address': 'USA'}, function (results, status) {

                        var bounds = new google.maps.LatLngBounds();
                        for (var j = 0; j < locations.length; j++) {

                        startingPoint = new google.maps.LatLng(getLatLong('565 W. Adams, Chicago, IL 60661'));
                        var mapOptions = {
                          zoom: 14,
                          center: startingPoint,
                          mapTypeId: google.maps.MapTypeId.ROADMAP

                        var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

                        var marker = new google.maps.Marker({
                              map: map,
                              icon: "",
                              position: startingPoint

                    function codeAddress(address) {
                        geocoder.geocode( { 'address': address}, function(results, status) {
                          if (status == google.maps.GeocoderStatus.OK) {
                              var location = results[0].geometry.location;

                          } else {
                              alert('Geocode was not successful for the following reason: ' + status);

                  function getLatLong(address)
                        var geocoder = new google.maps.Geocoder();
                        var result = "";
                        geocoder.geocode( { 'address': address}, function(results, status) {
                              if (status == google.maps.GeocoderStatus.OK) {
                                    result = results[0].geometry.location;
                              } else {
                                    result = "Unable to find address: " + status;
                        console.log("TEST" + address + " " + result);

                    function compareDistances(loc){
                        latlngs.push(loc);//add markers to the latlngs array
                        //when the markers array length matches the locations array length...
                        if (latlngs.length == locations.length){
                              var distances = [];
                              //use geometry.spherical to calculate the distances between pairs of lat/lng coordinates
                              for(var j = 0; j < latlngs.length; j++){
                                    distances.push({distance:google.maps.geometry.spherical.computeDistanceBetween(startingPoint, latlngs[j]), marker: j});
                              //reorder the distances, shortest first, closest will then be distances[0] and distances[1]
                              distances.sort(function(a,b) {
                                    return a.distance - b.distance;
                              //add the two closest markers
                              var marker1 = new google.maps.Marker({map: map, position: latlngs[distances[0].marker]});
                              var marker2 = new google.maps.Marker({map: map, position: latlngs[distances[1].marker]});
                              //extend the bounds of the map to include the two closest markers and the starting point
                              //zoom the map to nicely fit the bounds of the two closest markers and the starting point


    <div id="map-canvas"></div>
Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros