Google Direction service giving wrong route

504 Views Asked by At

Drawing googlemap route on map with below coordinates.But getting wrong route. Where is the issue?

LNG                    LAT
58.5589893333333       23.6229513333333   Start
58.5589985             23.6231225         WAY POINT 1
58.5591366666667       23.6235491666667   WAY POINT 2
58.55882               23.6236481666667   WAY POINT 3
58.5476361666667       23.6209141666667   WAY POINT 4
58.5454098333333       23.616038          END

the wrong route

Same coordinates tried in Google map website,It is giving correct way

Where is the problem? Sending all the waypoints, origin and destination correctly. The coordinates are from tracking devices. only some cases showing wrong route like this.

var directionDisplay;
var directionsService = new google.maps.DirectionsService();
directionsDisplay = new google.maps.DirectionsRenderer();     
directionsDisplay.setMap(map);
var first = new google.maps.LatLng(23.6231225,58.5589985);
var second = new google.maps.LatLng(23.6235491666667,58.5591366666667);
var third = new google.maps.LatLng(23.6236481666667,58.55882);
var forth = new google.maps.LatLng(23.6209141666667,58.5476361666667); 
var i = 0;
var start = StartlatDir[i]+','+StartlongDir[i];
var end = EndlatDir[i] + ',' + EndlongDir[i];
var request = {
    origin: start,
    destination: end,
    waypoints: [{ location:first, stopover: false },
        { location: second, stopover: false },
        { location: third, stopover: false },
        { location: forth, stopover: false }],
    optimizeWaypoints: false,
    travelMode: google.maps.DirectionsTravelMode.DRIVING,
    unitSystem: google.maps.UnitSystem.METRIC
};
directionsService.route(request, function (response, status) {
    if (status == google.maps.DirectionsStatus.OK) {
        directionsDisplay.setDirections(response);
        var myRoute = response.routes[0];
        var txtDir = '';
        for (var i = 0; i < myRoute.legs[0].steps.length; i++) {
            txtDir += myRoute.legs[0].steps[i].instructions + "<br />";
        }
    }
});
1

There are 1 best solutions below

4
geocodezip On

The DirectionsService is very sensitive to waypoints near exits (or on the wrong side of the road).

Waypoint 4 (23.6209141666667, 58.5476361666667) makes the route take the exit.

original route

Moving it further along the road to (23.620315,58.5471) gives the expected route:

fiddle

map with waypoint 4 after the exit or removing it completely gives the expected route

fiddle

map with waypoint 4 removed