Search code examples
ruby-on-railsrubygmaps4rails

How to getting directions in gmaps4rails v2?


i have researched many sources and could not find any help in getting directions in the v2 of gmaps4rails. i would like to either get directions in a text format (step by step list) or just show the directions on my map it self. I would like my starting point to be a lat and long and the destination also to be a lat and long. thank you any help is appreciated.


Solution

  • It's pretty straightforward from google example here:

    var directionsDisplay = new google.maps.DirectionsRenderer();
    var directionsService = new google.maps.DirectionsService();
    
    function calcRoute() {
      var origin      = new google.maps.LatLng(41.850033, -87.6500523);
      var destination = new google.maps.LatLng(42.850033, -85.6500523);
      var request = {
          origin:      origin,
          destination: destination,
          travelMode:  google.maps.TravelMode.DRIVING
      };
      directionsService.route(request, function(response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
          directionsDisplay.setDirections(response);
        }
      });
    }
    
    calcRoute(); // call this anywhere you want
    
    var handler = Gmaps.build('Google');
    handler.buildMap({ internal: {id: 'map'}}, function(){
      directionsDisplay.setMap(handler.getMap());
    });