2013-03-14 83 views
0

我使用谷歌地圖服務的方向如下:谷歌地圖導航服務 - 需要3米不同距離

directionsDisplay = new google.maps.DirectionsRenderer(); 

var directionsService = new google.maps.DirectionsService(); 

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

arrObjWaypoints = []; 

// Get waypoints 
if (arrObjLatLngs.length > 2) { 

    for (var i = 0; i < arrObjLatLngs.length; i++) { 

     arrObjWaypoints.push({ 
      location:arrObjLatLngs[i], 
      stopover:true} 
     ); 

    } 

} 

var request = { 
    origin:start, 
    destination:end, 
    travelMode: google.maps.DirectionsTravelMode.DRIVING, 
    unitSystem: google.maps.DirectionsUnitSystem.METRIC, 
    provideRouteAlternatives: false, 
    waypoints: arrObjWaypoints 
}; 

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

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

     if (boolShowMap) { 

      directionsDisplay.setMap(MapObject); 
      $('#cp-div-map').dialog('open'); 
      google.maps.event.trigger(MapObject, 'resize'); 
      directionsDisplay.setDirections(response); 

     } else { 

      var total_distance = 0; 

      // Add up distance of all available legs 
      for (var i = 0; i < response.routes[0].legs.length; i++) { 

       total_distance += parseInt(response.routes[0].legs[i].distance.value); 

      } 

      callBack(total_distance); 

     } 

    } 

}); 

基本上,第一位是好的,爲獲得startend之間的距離......但是,我還需要一個單獨的值來計算basestart之間的距離以及在baseend之間的第三距離。

我已經試過這樣: -

var directionsServicePickup = new google.maps.DirectionsService(); 

var base = "52.781048888889, -1.2110222222222546"; 

var request2 = { 
    origin:base, 
    destination:start, 
    travelMode: google.maps.DirectionsTravelMode.DRIVING, 
    unitSystem: google.maps.DirectionsUnitSystem.METRIC, 
    provideRouteAlternatives: false, 
    waypoints: arrObjWaypoints 
}; 


directionsServicePickup.route(request2, function(response, status) { 

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

      var total_pickup_distance = 0; 

      // Add up distance of all available legs 
      for (var i = 0; i < response.routes[0].legs.length; i++) { 

       total_pickup_distance += parseInt(response.routes[0].legs[i].distance.value); 

      } 

      var km = parseInt(total_pickup_distance)/1000; 

      // Convert KM into Miles 
      var pickup_distance = parseInt((km/8) * 5); 

      alert('Pickup='+pickup_distance); 

      var runin_pickup = 1200; 

      // Send to our checkRuninTariff function 
      checkRuninTariff(runin_pickup); 

     } 

}); 

但這似乎overridding,我發送的第一個請求。我對這個API不太熟悉,所以對此非常感激!

+0

如果你只需要行駛距離,你可以使用[DistanceMatrix(https://developers.google.com/maps/documentation/javascript/distancematrix)第二操作。 – geocodezip 2013-03-14 13:03:14

+0

對不起,我不明白你的意思是從不同的緯度/經度獲得結果。只要改變原點? – Rafe 2013-03-14 13:03:58

+0

基本上,我需要'origin:start'的距離,'to destination:start,'....但是我需要'origin:base'的單獨距離值到'destination:start' – nsilva 2013-03-14 13:09:45

回答