google-maps-tsp-solver

https://groups.google.com/forum/?hl=zh-CN&fromgroups#!forum/google-maps-tsp-solver

What you are doing should not work even with addWaypoint. You need to 
move tsp.getOrder() and tsp.getDurations() into the onSolveCallback 
function. The call to tsp.solveRoundTrip is asynchronous (so it 
returns before the computation is done). The results are not ready 
until onSolveCallback is called. 

Geir 

On Fri, May 17, 2013 at 3:22 PM, Cannect <[email protected]> wrote: 
> Hello all, 
> 
> Thanks for taking the time for me. The title describes it well, the setup 
> below works only with tsp.AddWaypoint(). Possible my adresses are wrong? 
> For me it is most important to get the trip order and total trip duration. 
> 
> var myOptions = { 
>                 zoom: 12, 
>                 center: new google.maps.LatLng(52.119999,5.866699), 
>                 mapTypeId: google.maps.MapTypeId.ROADMAP 
>             }; 
>             myMap = new google.maps.Map(document.getElementById("map"), 
> myOptions); 
> 
>             directionsPanel = document.getElementById("routebeschrijving"); 
> 
>             tsp = new BpTspSolver(myMap, directionsPanel); 
> 
>             tsp.setTravelMode(google.maps.DirectionsTravelMode.DRIVING); 
> 
>             tsp.addAddress('Amsterdam, the netherlands'); 
>             tsp.addAddress('ede, the netherlands'); 
>             tsp.addAddress('arnhem, the netherlands'); 
>             tsp.addAddress('Groningen, the netherlands'); 
> 
>             tsp.solveRoundTrip(onSolveCallback); 
> 
>             var order = tsp.getOrder(); 
>             var durations = tsp.getDurations(); 
>             console.log(order); 
>             console.log(durations); 
> 
>         } 
> 
>     function onSolveCallback(theTsp){ 
>     } 
>     function addWaypointCallback(theTsp){ 
>     } 
> 

 

 

 

 

----------------------------------------------------

 <!DOCTYPE html>
    <html>
    <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
      html { height: 100% }
      body { height: 100%; margin: 0px; padding: 0px }
      #map_canvas { height: 100% }
    </style>
    
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript"
       src="http://maps.google.com/maps/api/js?sensor=false">
    </script>
    
    <script type="text/javascript" src="BpTspSolver.js"></script>
    <script type="text/javascript" src="tsp.js"></script>
    
    <script type="text/javascript">
    function initialize() {
    
    // Your normal Google Map object initialization
    var myOptions = {
      zoom: 6,
      center: new google.maps.LatLng(43.773062, -81.531944 ),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    myMap = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
 
 
    directionsPanel = document.getElementById("my_textual_div");
    
    // Create the tsp object
    tsp = new BpTspSolver(myMap, directionsPanel);
    
    // Set your preferences
    tsp.setAvoidHighways(false);
    tsp.setTravelMode(google.maps.DirectionsTravelMode.DRIVING);
    
    // 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(new google.maps.LatLng(-34.397, 150.644), 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);
    
    tsp.addWaypoint(new google.maps.LatLng( 43,773062, -81.531944));  //Auburn
    tsp.addWaypoint(new google.maps.LatLng(43.553447, -81.393425 ));  //Seaforth
    tsp.addWaypoint(new google.maps.LatLng(43.729708, -81.60856));    //Goderich
    tsp.addWaypoint(new google.maps.LatLng(43.677386, -81.30209));    //Walton
    tsp.addWaypoint(new google.maps.LatLng(43.72219, -81.625006));    //Benmiller
    tsp.addWaypoint(new google.maps.LatLng(43.721352, -81.433582));   //Blyth
    tsp.addWaypoint(new google.maps.LatLng(43.85478, -81.599622));    //Dungannon
    tsp.addWaypoint(new google.maps.LatLng(43.617079, -81.539712));   //Clinton
   
    
    // 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.
    
    }
    
    </script>
    </head>
    <body onload="initialize()">
    <div id="map_canvas" style="width:700px; height:700px"></div>
    <div id="my_textual_div"></div>
 <div id="path" ></div>
 <div id="exportDataButton" ></div>
 <div id="durations" ></div>
 <div id="routeDrag" ></div>
 <div id="my_textual_div" ></div>
 <div id="tomtom" ></div>
 <div id="exportGoogle" ></div>
 <div id="reverseRoute" ></div>
 
 
    </body>
    </html>

你可能感兴趣的:(Google)