Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
    });
like image 268
Santiago Avatar asked Mar 08 '26 00:03

Santiago


1 Answers

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

like image 58
geocodezip Avatar answered Mar 10 '26 04:03

geocodezip