troubleshooting Question

Google Maps API - stack overflow at line 26

Avatar of saturation
saturation asked on
JavaScript
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>
<html>
  <head>
    <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% }
      </style>
      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
    <script src="http://maps.googleapis.com/maps/api/js?sensor=false&v=3&libraries=geometry" type="text/javascript"></script>

    <script>
            $(document).ready(function(){
                    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) {
                              map.fitBounds(results[0].geometry.viewport);
                        });

                        var bounds = new google.maps.LatLngBounds();
                        for (var j = 0; j < locations.length; j++) {
                              codeAddress(locations[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: "http://www.conceptfire-uk.com/wp-content/uploads/2011/07/icon-pin-color.png",
                              position: startingPoint
                        });


                    function codeAddress(address) {
                        geocoder.geocode( { 'address': address}, function(results, status) {
                          if (status == google.maps.GeocoderStatus.OK) {
                              var location = results[0].geometry.location;
                              compareDistances(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;
                                    alert(result);
                              } 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
                              bounds.extend(startingPoint);
                              bounds.extend(latlngs[distances[0].marker]);
                              bounds.extend(latlngs[distances[1].marker]);
                              //zoom the map to nicely fit the bounds of the two closest markers and the starting point
                              map.fitBounds(bounds);
                        }
                    }
            });

    </script>
  </head>
  <body>

    <div id="map-canvas"></div>
  </body>
</html>
ASKER CERTIFIED SOLUTION
leakim971
Multitechnician
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