Google Direction | same API, different result
<p>I have a simple route I made on JavaScript on the following link: </p>

<p><a href="http://debug.iott.co.il/map/route.v.1.0.html" rel="nofollow noreferrer">http://debug.iott.co.il/map/route.v.1.0.html</a>
<a href="https://i.stack.imgur.com/EWKat.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/EWKat.png" alt="enter image description here"></a></p>

<p><em>edit</em> here is the image from JavaScript for the lazy ones.</p>

<p>it's goes from A to B, B to C and C to D.
watch this, when the User hit the point C he need to do a U-turn to get to point D, which is fine by me.</p>

<p>I have tried to duplicate the same thing on android studio, and i'm getting different route like the following image:</p>

<p><a href="https://i.stack.imgur.com/NlQdt.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NlQdt.png" alt="android example"></a></p>

<p>so instead of doing a U-turn from C to D, it's makes the user to goto all the way to A again, then B and lastly D.
I'm trying to figure out why i'm using the same API (Google Direction) with the same Configuration (Driving Mode etc.) and i'm getting different results.
any ideas ? </p>

<p>Edit 2: Here is some of the code: 
JavaScript :</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>//You can calculate directions (using a variety of methods of transportation) by using the DirectionsService object.
var directionsService = new google.maps.DirectionsService();

//Define a variable with all map points.
var _mapPoints = new Array();

//Define a DirectionsRenderer variable.
var _directionsRenderer = '';

//This will give you the map zoom value.
var zoom_option = 6;

//LegPoints is your route points between two locations.
var LegPoints = new Array();

var currentPOI = 0;
//Google map object
var map;

var route = {
    'ID': '768cbe75-cade-4498-95c5-7369e8ac9d20',
    'Name': 'BY Route 1',
    'Points': [{
        'Latitude': 32.33197427500548,
        'Longtitude': 34.894479140639305,
        'Direction': 0,
        'TimeStamp': '\/Date(1465923981572+0300)\/',
        'Icon': ''
    }, {
        'Latitude': 32.330265,
        'Longtitude': 34.893424,
        'Direction': 0,
        'TimeStamp': '\/Date(1465923981572+0300)\/',
        'Icon': 'L'
    }, {
        'Latitude': 32.329581,
        'Longtitude': 34.894216,
        'Direction': 0,
        'TimeStamp': '\/Date(1465923981572+0300)\/',
        'Icon': 'U'
    }, {
        'Latitude': 32.330517,
        'Longtitude': 34.892191,
        'Direction': 0,
        'TimeStamp': '\/Date(1465923981572+0300)\/',
        'Icon': 'L'
    }, {
        'Latitude': 32.329863,
        'Longtitude': 34.890681,
        'Direction': 0,
        'TimeStamp': '\/Date(1465923981572+0300)\/',
        'Icon': 'R'
    }, {
        'Latitude': 32.333253,
        'Longtitude': 34.890102,
        'Direction': 0,
        'TimeStamp': '\/Date(1465923981572+0300)\/',
        'Icon': 'R'
    }, {
        'Latitude': 32.333216,
        'Longtitude': 34.892721,
        'Direction': 0,
        'TimeStamp': '\/Date(1465923981572+0300)\/',
        'Icon': 'L'
    }, {
        'Latitude': 32.337219,
        'Longtitude': 34.893506,
        'Direction': 0,
        'TimeStamp': '\/Date(1465923981572+0300)\/',
        'Icon': 'R'
    }, {
        'Latitude': 32.337473,
        'Longtitude': 34.893954,
        'Direction': 0,
        'TimeStamp': '\/Date(1465923981572+0300)\/',
        'Icon': 'L'
    }, {
        'Latitude': 32.337336,
        'Longtitude': 34.893433,
        'Direction': 0,
        'TimeStamp': '\/Date(1465923981572+0300)\/',
        'Icon': 'L'
    }, {
        'Latitude': 32.337335,
        'Longtitude': 34.894009,
        'Direction': 0,
        'TimeStamp': '\/Date(1465923981572+0300)\/',
        'Icon': 'R'
    }, {
        'Latitude': 32.337281,
        'Longtitude': 34.894530,
        'Direction': 0,
        'TimeStamp': '\/Date(1465923981572+0300)\/',
        'Icon': 'R'
    }, {
        'Latitude': 32.335042,
        'Longtitude': 34.894198,
        'Direction': 0,
        'TimeStamp': '\/Date(1465923981572+0300)\/',
        'Icon': 'L'
    }, {
        'Latitude': 32.335056,
        'Longtitude': 34.894756,
        'Direction': 0,
        'TimeStamp': '\/Date(1465923981572+0300)\/',
        'Icon': 'R'
    }, {
        'Latitude': 32.332629,
        'Longtitude': 34.894617,
        'Direction': 0,
        'TimeStamp': '\/Date(1465923981572+0300)\/',
        'Icon': 'R'
    }, {
        'Latitude': 32.333123,
        'Longtitude': 34.893378,
        'Direction': 0,
        'TimeStamp': '\/Date(1465923981572+0300)\/',
        'Icon': 'R'
    }, {
        'Latitude': 32.335002,
        'Longtitude': 34.894187,
        'Direction': 0,
        'TimeStamp': '\/Date(1465923981572+0300)\/',
        'Icon': 'R'
    }, {
        'Latitude': 32.335120,
        'Longtitude': 34.895929,
        'Direction': 0,
        'TimeStamp': '\/Date(1465923981572+0300)\/',
        'Icon': 'R'
    }, {
        'Latitude': 32.331783,
        'Longtitude': 34.895945,
        'Direction': 0,
        'TimeStamp': '\/Date(1465923981572+0300)\/',
        'Icon': 'R'
    }, {
        'Latitude': 32.332448,
        'Longtitude': 34.894796,
        'Direction': 0,
        'TimeStamp': '\/Date(1465923981572+0300)\/',
        'Icon': 'L'
    }]
};
//InitializeMap() function is used to initialize google map on page load.
function InitializeMap() {

    //DirectionsRenderer() is a used to render the direction
    _directionsRenderer = new google.maps.DirectionsRenderer({
        draggable: true,
        map: map,
        panel: document.getElementById('right-panel')
    });

    //Set the your own options for map.
    var myOptions = {
        zoom: zoom_option,
        zoomControl: true,
        center: new google.maps.LatLng(32.2679, 34.3718),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    //Define the map.
    map = new google.maps.Map(document.getElementById("dvMap"), myOptions);

    //Set the map for directionsRenderer
    _directionsRenderer.setMap(map);

    //Set different options for DirectionsRenderer mehtods.
    //draggable option will used to drag the route.
    _directionsRenderer.setOptions({
        draggable: false
    });

    ////Add the doubel click event to map.
    //google.maps.event.addListener(map, "dblclick", function (event) {
        
    //    var _currentPoints = event.latLng;
    //    _mapPoints.push(_currentPoints);
    //    LegPoints.push('');
    //    getRoutePointsAndWaypoints(_mapPoints);
    //});

    //Add the directions changed event to map.
    google.maps.event.addListener(_directionsRenderer, 'directions_changed', function() {
        var myroute = _directionsRenderer.directions.routes[0];
        CreateRoute(myroute);
        zoom_option = map.getZoom();
    });

   InitialRoute();
}

function InitialRoute() {
 var latlon = new google.maps.LatLng();
  
 
    for (var i = currentPOI; i &lt; route.Points.length &amp;&amp; i &lt; currentPOI+10; i++) {
        latlon = new google.maps.LatLng(route.Points[i].Latitude, route.Points[i].Longtitude);
        _mapPoints.push(latlon);
        LegPoints.push(latlon);
        window.setTimeout(function () { getRoutePointsAndWaypoints(_mapPoints); }, 100);
    }
    currentPOI += 10;
    if (currentPOI &gt; route.Points.length) {
        currentPOI = 0;

    }

}




function computeTotalDistance(result) {
    var total = 0;
    var myroute = result.routes[0];
    for (var i = 0; i &lt; myroute.legs.length; i++) {
        total += myroute.legs[i].distance.value;
    }
    total = total / 1000;
    document.getElementById('total').innerHTML = total + ' km';
}
function CreateRoute(myroute) {

    var index = 0;
    if (_mapPoints.length &gt; 10) {
        index = _mapPoints.length - 10;
    }

    for (var i = 0; i &lt; myroute.legs.length; i++) {
        saveLegPoints(myroute.legs[i], index);
        index = index + 1;
    }
}

//Saving the all the legs points between two routes
function saveLegPoints(leg, index) {
    var points = new Array();
    for (var i = 0; i &lt; leg.steps.length; i++) {
        for (var j = 0; j &lt; leg.steps[i].lat_lngs.length; j++) {
            points.push(leg.steps[i].lat_lngs[j]);
        }
    }
    LegPoints[index] = points;
}

//This will draw the more then 10 points route on map.
function drawPreviousRoute(Legs) {
    var segPointValue = new Array();
    for (var i = 0; i &lt; Legs; i++) {
        var innerArry = LegPoints[i];
        for (var j = 0; j &lt; innerArry.length; j++) {
            segPointValue.push(innerArry[j]);
        }
        addPreviousMarker(innerArry[0],i);
    }
    var polyOptions = {
        path: segPointValue,
        strokeColor: '#FFCC00',
        strokeWeight: 5
    };
    var poly = new google.maps.Polyline(polyOptions);
    poly.setMap(map);
}

//This wil add the marker icon to the route.
function addPreviousMarker(myLatlng,id) {

    var pinImage = new google.maps.MarkerImage("http://chart.apis.google.com/chart?chst=d_map_pin_letter&amp;chld=" + id + "|FFCC00|0000FF");
    //var pinImage = {
    //    url: "http://chart.apis.google.com/chart?chst=d_map_pin_letter&amp;chld=" + id + "|FFCC00|0000FF"
    //    //size: new google.maps.Size(71, 71),
    //    //origin: new google.maps.Point(0, 0),
    //    //anchor: new google.maps.Point(17, 34),
    //    //scaledSize: new google.maps.Size(25, 25)
    //};
    var marker = new google.maps.Marker({
        position: myLatlng,
        icon: pinImage,//"Images/red-circle.png",
        title: ""
    });
    marker.setMap(map);
}


//getRoutePointsAndWaypoints() will help you to pass points and waypoints to drawRoute() function
function getRoutePointsAndWaypoints(Points) {
    if (Points.length &lt;= 10) {
        drawRoutePointsAndWaypoints(Points);
    }
    else {
        var newPoints = new Array();
        var startPoint = Points.length - 10;
        var Legs = Points.length - 10;
        for (var i = startPoint; i &lt; Points.length; i++) {
            newPoints.push(Points[i]);
        }
        drawRoutePointsAndWaypoints(newPoints);
        drawPreviousRoute(Legs);
    }
}


function drawRoutePointsAndWaypoints(Points) {
    //Define a variable for waypoints.
    var _waypoints = new Array();

    if (Points.length &gt; 2) //Waypoints will be come.
    {
        for (var j = 1; j &lt; Points.length - 1; j++) {
            var address = Points[j];
            if (address !== "") {
                _waypoints.push({
                    location: address,
                    stopover: true  //stopover is used to show marker on map for waypoints
                });
            }
        }
        //Call a drawRoute() function
        drawRoute(Points[0], Points[Points.length - 1], _waypoints);
    } else if (Points.length &gt; 1) {
        //Call a drawRoute() function only for start and end locations
        drawRoute(Points[_mapPoints.length - 2], Points[Points.length - 1], _waypoints);
    } else {
        //Call a drawRoute() function only for one point as start and end locations.
        drawRoute(Points[_mapPoints.length - 1], Points[Points.length - 1], _waypoints);
    }
}





//drawRoute() will help actual draw the route on map.
function drawRoute(originAddress, destinationAddress, _waypoints) {
    //Define a request variable for route .
    var _request = '';

    //This is for more then two locatins
    if (_waypoints.length &gt; 0) {
        _request = {
            origin: originAddress,
            destination: destinationAddress,
            waypoints: _waypoints, //an array of waypoints
            optimizeWaypoints: false, //set to true if you want google to determine the shortest route or false to use the order specified.
            travelMode: google.maps.DirectionsTravelMode.DRIVING
        };
    } else {
        //This is for one or two locations. Here noway point is used.
        _request = {
            origin: originAddress,
            destination: destinationAddress,
            travelMode: google.maps.DirectionsTravelMode.DRIVING
        };
    }

    //This will take the request and draw the route and return response and status as output
    directionsService.route(_request, function(_response, _status) {
        if (_status == google.maps.DirectionsStatus.OK) {
            _directionsRenderer.setDirections(_response);
            computeTotalDistance(_directionsRenderer.getDirections());

        }
    });
}</code></pre>
</div>
</div>
</p>

<p>The Code in Android is a simple Routing Call using Library.</p>

<pre><code>  Routing routing = new Routing.Builder()
                .travelMode(Routing.TravelMode.DRIVING)
                .withListener(this)
                .waypoints(firstEightWayPoints)
                .language("iw")
                .build();
        routing.execute();
</code></pre>