How to implement TSP Solver for google maps

526 views
Skip to first unread message

Nick Tunac

unread,
Feb 4, 2014, 5:25:49 AM2/4/14
to google-maps...@googlegroups.com
The example is here, but I cannot completely understand on how to implement it.
Can you please guide me, in implementing this to my project? Thank you
// Your normal Google Map object initialization
var myOptions = {
  zoom
: zoom,
  center
: center,
  mapTypeId
: google.maps.MapTypeId.ROADMAP
};
myMap
= new google.maps.Map(div, myOptions);
directionsPanel
= document.getElementById("my_textual_div");

// Create the tsp object
tsp
= new BpTspSolver(myMap, directionsPanel);

// Set your preferences
tsp
.setAvoidHighways(true);
tsp
.setTravelMode(google.maps.DirectionsTravelMode.WALKING);

// Add points (by coordinates, or by address).
// The first point added is the starting location.
// The last point added is the final destination (in the case of A - Z mode)
tsp
.addWaypoint(latLng, addWaypointCallback);  // Note: The callback is new for version 3, to ensure waypoints and addresses appear in the order they were added in.
tsp
.addAddress(address, addAddressCallback);

// Solve the problem (start and end up at the first location)
tsp
.solveRoundTrip(onSolveCallback);
// Or, if you want to start in the first location and end at the last,
// but don't care about the order of the points in between:
tsp
.solveAtoZ(onSolveCallback);

// Retrieve the solution (so you can display it to the user or do whatever :-)
var dir = tsp.getGDirections();  // This is a normal GDirections object.
// The order of the elements in dir now correspond to the optimal route.

// If you just want the permutation of the location indices that is the best route:
var order = tsp.getOrder();

// If you want the duration matrix that was used to compute the route:
var durations = tsp.getDurations();

// There are also other utility functions, see the source.

דוב פלדמן

unread,
Aug 26, 2014, 9:44:30 PM8/26/14
to google-maps...@googlegroups.com
what i must to add to the code to show the direction on the google map?

בתאריך יום שלישי, 4 בפברואר 2014 12:25:49 UTC+2, מאת Nick Tunac:
Reply all
Reply to author
Forward
0 new messages