Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Distance between two locations - Google Maps

I'm trying to find the location between two points using google maps. Here is the code that I'm working with:

function initialize() {
          var myOptions = {
            center: new google.maps.LatLng(36.8813329,-103.6975488),
            zoom: 4,
            mapTypeId: google.maps.MapTypeId.ROADMAP
          };
          var map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);

           var impactCoordinates = [
              new google.maps.LatLng(37.772323, -122.214897),
              new google.maps.LatLng(34.1633766,-81.6487862),
                             ];
            var ImpactPath = new google.maps.Polyline({
              path: impactCoordinates,
              strokeColor: "#FF0000",
              strokeOpacity: 1.0,
              strokeWeight: 2
            });

            ImpactPath.setMap(map);

            var loc1 = new google.maps.LatLng(37.772323, -122.214897);
            var loc2 = new google.maps.LatLng(34.1633766,-81.6487862);

            alert(google.maps.geometry.spherical.computeDistanceBetween(loc1, loc2));
        }

This is the error I get from the console:

Uncaught TypeError: Cannot read property 'spherical' of undefined

like image 681
cclerv Avatar asked Apr 18 '12 14:04

cclerv


People also ask

How do I draw a distance on Google Maps?

Open Google Maps and right-click on a starting point. On the menu that appears, click “Measure Distance.” 2. Click anywhere on the map to draw a line between the starting point and the destination point.


1 Answers

If you haven't done so, explicitly add the geometry library in your <script> tag src="http://maps.googleapis.com/maps/api/js?libraries=geometry&sensor=false">

like image 138
Heitor Chang Avatar answered Oct 09 '22 01:10

Heitor Chang