2012-04-18 36 views
22

Estoy tratando de encontrar la ubicación entre dos puntos usando Google Maps. Aquí está el código que estoy trabajando con:Distancia entre dos ubicaciones: Google Maps

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)); 
     } 

Este es el error que recibo de la consola:

Uncaught TypeError: Cannot read property 'spherical' of undefined

Respuesta

48

Si no lo ha hecho, agregar explícitamente la biblioteca en su geometría <script> etiqueta src="http://maps.googleapis.com/maps/api/js?libraries=geometry&sensor=false">

+2

¡Gracias! Esto es exactamente lo que me estaba perdiendo. – cclerville

+0

También es necesario usar la URL anterior; es decir, 'http: //maps.google.com/maps/api/js? sensor = false &? libraries = geometry' no funcionará. – Arthur

+0

@Arthur es porque olvidó eliminar el signo de interrogación. '&? libraries = geometry' debe ser' & libraries = geometry' – jimmystormig

Cuestiones relacionadas