Coskun Coskun
Coskun Coskun

Reputation: 23

shortest route when using waypoints

i can use waypoints perfectyl.but i can not get shortest directions.i try to use route alternatives but it doesn't work. i need some thing like this : http://i58.tinypic.com/2vjbt6d.jpg

is there any way ?

my codes

function codeAddress(adres) {
        var address = adres;  
        geocoder.geocode({ 'address': address }, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                map.setCenter(results[0].geometry.location);
            }
        });

    }

    function calcRoute() {
        var grid = document.getElementById('GridView1');
        var start = document.getElementById('DropDownList_ilce').value + "," + document.getElementById('DropDownList_il').value;
        var end = document.getElementById('GridView1').rows[grid.rows.length - 1].cells[4].innerHTML + "," + document.getElementById('GridView1').rows[grid.rows.length - 1].cells[3].innerHTML;

        var waypts = [];
        for (var i = 0; i < grid.rows.length - 2; i++) {
            waypts.push({
                location: document.getElementById('GridView1').rows[i+1].cells[4].innerHTML + "," + document.getElementById('GridView1').rows[i+1].cells[3].innerHTML,
                stopover: true
            });
        }


        var request = {
            origin: start,
            destination: end,
            waypoints: waypts,
            optimizeWaypoints: true,
            travelMode: google.maps.TravelMode.DRIVING
        };
        directionsService.route(request, function (response, status) {
            if (status == google.maps.DirectionsStatus.OK) {
                directionsDisplay.setDirections(response);
            }
        });

Upvotes: 1

Views: 764

Answers (1)

MrUpsidown
MrUpsidown

Reputation: 22487

As I said in my comment, the API seems to give no option parameter to return the shortest route by default.

The key here is to use provideRouteAlternatives: true as DirectionsRequest property:

var request = {
    origin: 'cerkeş,çankırı',
    destination: 'diyarbakır',
    travelMode: google.maps.DirectionsTravelMode.DRIVING,
    provideRouteAlternatives: true
};

For the given origin and destination, this will return 2 separate routes. One of 970km and one of 1137 km.

Then you will have to calculate which route is the shortest. You could do something like that:

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

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

        var distance = null;
        var routeIndex = 0;

        // Loop through the routes to find the shortest one
        for (var i=0; i<response['routes'].length; i++) {

            var routeDistance = response['routes'][i].legs[0].distance.value;

            if (distance === null) {

                distance = routeDistance;
                routeIndex = i;
            }

            if (routeDistance < distance) {

                distance = routeDistance;
                routeIndex = i;
            }
        }

        directionsDisplay.setDirections(response);

        // Set route index
        directionsDisplay.setOptions({ 
            routeIndex: routeIndex
        });
    }
});

Note that when you have multiple routes, the key is to set the route index of the one you want to show.

// Set route index
directionsDisplay.setOptions({
    routeIndex: routeIndex
});

This example doesn't use waypoints. I believe that if you use waypoints, you will end up with multiple DirectionsLeg legs. In which case, you will have to do a bit more calculation to add each leg distance to find the total route distance.

Hope this helps!

Upvotes: 1

Related Questions