GMaps.js : Trace route between two markers set by geocoding

Santiago picture Santiago · Aug 6, 2012 · Viewed 8.3k times · Source

I'm using this: http://hpneo.github.com/gmaps/examples.html

I have two inputs where I put an address and I want to trace a route between the two points. I can add two markers, but I dont know how could I draw the route.

Any idea on how to do this?

Thanks!

This is my code...

    GMaps.geocode({
      address: address_from,
      callback: function(results, status) {
        if (status == 'OK') {
          var address_from = results[0].geometry.location;
          map.addMarker({
            lat: address_from.lat(),
            lng: address_from.lng()
          });
        }
      }       
    });

    GMaps.geocode({
      address: address_to,
      callback: function(results, status) {
        if (status == 'OK') {
          var address_to = results[0].geometry.location;
          map.addMarker({
            lat: address_to.lat(),
            lng: address_to.lng()
          });
        }
      }
    });

    map.drawRoute({
      origin: [address_from.lat(), address_from.lng()],
      destination: [address_to.lat(), address_to.lng()],
      travelMode: 'driving',
      strokeColor: '#131540',
      strokeOpacity: 0.6,
      strokeWeight: 6
    });

Answer

geocodezip picture geocodezip · Aug 6, 2012

This example from this similar question gets directions between two markers placed on the map by clicking (uses the Google Maps API v3 directly).