2

How to compute shortest route between two points in google map that user selects and provide all routes from source to destination in Transport system?

1 Answer 1

5

you can achieve this with Google Maps JavaScript API v3 - Directions Service. There is a doc here. and you can see stand-alone google example here.

and in this doc. you can get information about Travel Modes.

When you calculate directions, you need to specify which transportation mode to use. The following travel modes are currently supported:

TravelMode.DRIVING indicates standard driving directions using the road network.
TravelMode.WALKING requests walking directions via pedestrian paths & sidewalks 
(where available).
New! TravelMode.BICYCLING requests bicycling directions via bicycle paths & preferred
streets (currently only available in the US).

the Directions Service code:

var directionDisplay;
  var directionsService = new google.maps.DirectionsService();
  var map;

  function initialize() {
    directionsDisplay = new google.maps.DirectionsRenderer();
    var chicago = new google.maps.LatLng(41.850033, -87.6500523);
    var myOptions = {
      zoom:7,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      center: chicago
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    directionsDisplay.setMap(map);
  }

  function calcRoute() {
    var start = document.getElementById("start").value;
    var end = document.getElementById("end").value;
    var request = {
        origin:start, 
        destination:end,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    };
    directionsService.route(request, function(response, status) {
      if (status == google.maps.DirectionsStatus.OK) {
        routePath = result.routes[0].overview_path;
        for(var a = 0; a< routePath.length; a++){
          // Your vector layer to render points with line
        }
        directionsDisplay.setDirections(response);
      }
    });
  }

The above code was taken from google api, i added some code there for you which you should have use used for your app.. main code for you is here:

directionsService.route(request, function(response, status) {
          if (status == google.maps.DirectionsStatus.OK) {
            routePath = result.routes[0].overview_path;
            for(var a = 0; a< routePath.length; a++){
              // Your vector layer to render points with line
            }
            directionsDisplay.setDirections(response);
          }
        });

i hope it helps you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.