2012-07-17 81 views
5

我有一張路線圖(多個目的地 - 不只是A,B,如下面的頁面)。方向是可拖動的。我想保存更改後的路線。下面的頁面是什麼,我經過這裏......Gmaps路線保存路線,航點

http://vikku.info/programming/google-maps-v3/draggable-directions/saving-draggable-directions-saving-waypoints-google-directions-google-maps-v3.htm

下面是我得到了什麼:

google.maps.event.addListener(directionsDisplay, 'directions_changed', function() { 
      console.log('reroute'); 

      var rleg_count = directionsDisplay.directions.routes[0].legs.length; 

      data.start = { 
       'lat':directionsDisplay.directions.routes[0].legs[0].start_location.lat(), 
       'lng':directionsDisplay.directions.routes[0].legs[0].start_location.lng() 
      }; 
      data.end = { 
       'lat':directionsDisplay.directions.routes[0].legs[rleg_count-1].end_location.lat(), 
       'lng':directionsDisplay.directions.routes[0].legs[rleg_count-1].end_location.lng() 
      }; 

      var wp=[]; 
      var w = []; 
      var route = directionsDisplay.directions.routes[0]; 
      for (var l = 0; l < route.legs.length; l++) 
      { 
       for(var j = 0; j < route.legs[l].via_waypoints.length; j++) 
       { 
        w.push({ 
         location:{'lat':route.legs[l].via_waypoints[j].lat(), 'lng':route.legs[l].via_waypoints[j].lng()}, 
         stopover:true 
        }); 
       } 
      } 

      data.waypoints = w; 
     }); 

而且SetRoute功能:

function setRoute(os) 
    { 
     var wp = []; 
     for(var i=0;i<os.waypoints.length;i++) 
     { 
      wp[i] = { 
       'location': new google.maps.LatLng(os.waypoints[i].location.lat, os.waypoints[i].location.lng), 
       'stopover': os.waypoints[i].stopover 
      } 
     } 

     var request = { 
      'origin':new google.maps.LatLng(os.start.lat, os.start.lng), 
      'destination':new google.maps.LatLng(os.end.lat, os.end.lng), 
      'waypoints': wp, 
      optimizeWaypoints: false, 
      avoidHighways: false, 
      avoidTolls: false, 
      travelMode: google.maps.TravelMode.DRIVING, 
     } 

     directionsService.route(request, function(res,sts){ 
      // console.log(res); 
      // console.log(sts); 
      if(sts=='OK') 
       directionsDisplay.setDirections(res); 
     }); 
    } 

的問題是如果只有2個目的地(A,B),則代碼有效,但只要有更多的目的地...它不能正常工作......它會忽略其他目的地

據我已經能夠告訴航點是正確的。似乎有多個目的地的問題,我不知道如何糾正它。

謝謝!

回答

0

我已成功地用下面的更新我的代碼解決這個問題:

google.maps.event.addListener(directionsDisplay, 'directions_changed', function() { 
      console.log('reroute'); 

      var rleg_count = directionsDisplay.directions.routes[0].legs.length; 

      data.start = { 
       'lat':directionsDisplay.directions.routes[0].legs[0].start_location.lat(), 
       'lng':directionsDisplay.directions.routes[0].legs[0].start_location.lng() 
      }; 
      data.end = { 
       'lat':directionsDisplay.directions.routes[0].legs[rleg_count-1].end_location.lat(), 
       'lng':directionsDisplay.directions.routes[0].legs[rleg_count-1].end_location.lng() 
      }; 

      var wp=[]; 
      var w = []; 
      var route = directionsDisplay.directions.routes[0]; 
      for (var l = 0; l < route.legs.length; l++) 
      { 
        // Skip first and last leg - for more than 2 legs 
      if(l != 0 || l != rleg_count-1) 
      { 
/* start solution */ 
       w.push({ 
        location:{'lat':route.legs[l].start_location.lat(), 'lng':route.legs[l].start_location.lng()}, 
        stopover:true 
       }); 
      } 
/* end solution */ 
       for(var j = 0; j < route.legs[l].via_waypoints.length; j++) 
       { 
        w.push({ 
         location:{'lat':route.legs[l].via_waypoints[j].lat(), 'lng':route.legs[l].via_waypoints[j].lng()}, 
         stopover:false 
        }); 
       } 
      } 

      data.waypoints = w; 
     });