IT_info
IT_info

Reputation: 727

Using waypoints to find the route

I am working on finding the route between two points using javascript, google maps api v3 and pgoruting. Now I have the following method which works fine when I give just one waypoint. BUT it does not work when I jave more than one waypoint. The format when there is more than one waypoint is delimeted with this symbol '|'. Therefore for example: 36.762121,14.7866553|35.988777778,14.655444333

The javascript method is the following:

   function calcRoute() {

        var all_nodes = document.getElementById('result').innerHTML;
        var node = all_nodes.split("|");

        var start = node[0];
        var end = node[node.length - 1];
        var wpts = [];

        for (var i = 1; i < node.length-1; i++) {
            wpts.push({
                location:node[i],
                stopover:true
            });
        }

        var request = {
            origin: start,
            destination: end,
            waypoints: wpts,
            travelMode: google.maps.TravelMode.DRIVING
        };

        directionsService.route(request, function (response, status) {
            if (status == google.maps.DirectionsStatus.OK) {
                directionsDisplay.setDirections(response);
            } else {
                alert('No route found');
            }
        });
    }      

Upvotes: 0

Views: 1349

Answers (2)

Rafe
Rafe

Reputation: 793

As previously suggested, it might help to get a google object for the location and it may also help to supply the lat and long as two seperate entities.

    for (var i = 1; i < node.length-1; i = i + 1) {
        node[i] = node[i].split(',');
        wpts.push({
            location:new google.maps.LatLng(node[i][0], node[i][1]),
            stopover:true
        });
    }

Upvotes: 0

David Chase
David Chase

Reputation: 2073

Actual thats incorrect waypoints are arrays of location:LatLng and stopover:true or false and they do not use the pipe delimiter please refer to Waypoints

Upvotes: 1

Related Questions