Maps API v3 Directions routes and steps

http://stackoverflow.com/questions/27700844 - How To get all latitude and longitude from source to destination place

by amitrdx

HTML

<script src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<div id="map-canvas"></div>

CSS

#map-canvas {
    height: 400px;
}

JavaScript

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

function initialize() {
    
    directionsDisplay = new google.maps.DirectionsRenderer();
    var center = new google.maps.LatLng(0, 0);
    var myOptions = {
        zoom: 7,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        center: center
    }

    map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);
    directionsDisplay.setMap(map);

    var source = new google.maps.LatLng('17.394986', '78.481517');
	var destination = new google.maps.LatLng('28.656492', '77.229139');
    
    var method = 'DRIVING';
    var request = {
        origin: source,
        destination: destination,
        travelMode: google.maps.DirectionsTravelMode[method],
        provideRouteAlternatives: false
    };

    directionsService.route(request, function (response, status) {

        if (status == google.maps.DirectionsStatus.OK) {

            var routesSteps = [];
            var routes = response.routes;
            var colors = ['red', 'green', 'blue', 'orange', 'yellow', 'black'];

            for (var i = 0; i < routes.length; i++) {

                new google.maps.DirectionsRenderer({
                    map: map,
                    directions: response,
                    routeIndex: i,
                    polylineOptions: {

                        strokeColor: colors[i],
                        strokeWeight: 4,
                        strokeOpacity: .3
                    }
                });

                var steps = routes[i].legs[0].steps;
                var stepsCoords = [];

                for (var j = 0; j < steps.length; j++) {

                    stepsCoords[j] = new google.maps.LatLng(steps[j].start_location.lat(), steps[j].start_location.lng());

                    new google.maps.Marker({
                        position: stepsCoords[j],
                        map: map,
                        icon: {
                          ...