2016-10-22 48 views
0

這是一個有點棘手的一個,我已經在現在幾個小時。谷歌地圖api不正確的計算

如果表格與出行航點一起使用,則「距離」和「成本」計算得很好。如果有航點,計算只能在「開始」地址和第一個航點之間進行。它完全忽略了目的地和其他方式點。

var inputs = document.getElementById('start'); 
var autocompletes = new google.maps.places.Autocomplete(inputs, options); 
var inpute = document.getElementById('end'); 
var autocompletee = new google.maps.places.Autocomplete(inpute, options); 

     directionsDisplay = new google.maps.DirectionsRenderer(); 
     var melbourne = new google.maps.LatLng(-31.953512, 115.857048); 
     var myOptions = { 
      zoom:12, 
      mapTypeId: google.maps.MapTypeId.ROADMAP, 
      center: melbourne 
     } 

     map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 
     directionsDisplay.setMap(map); 

    } 

    function calcRoute() { 
     var start = document.getElementById('start').value; 
     var end = document.getElementById('end').value; 
     if (waypoints.value) { 
     var waypts = []; 
     var optionsArray = document.getElementById("waypoints").value.split('\n'); 
     for (var i = 0; i < optionsArray.length; i++) { 
        if(optionsArray.length>0) { 
           waypts.push({ 
           location:optionsArray[i], 
           stopover:true}); 
        } 
     } 
     } 
     var distanceInput = document.getElementById("distance"); 
     var costInput = document.getElementById("cost"); 

if (waypoints.value) { 
     var request = { 
      origin:start, 
      destination:end, 
      waypoints: waypts, 
      optimizeWaypoints: true, 
      travelMode: google.maps.DirectionsTravelMode.DRIVING 
     }; 
     } else { 
     var request = { 
      origin:start, 
      destination:end, 
      optimizeWaypoints: true, 
      travelMode: google.maps.DirectionsTravelMode.DRIVING 
     }; 
} 
     directionsService.route(request, function(response, status) { 
      if (status == google.maps.DirectionsStatus.OK) { 
       directionsDisplay.setDirections(response); 
       distanceInput.value = response.routes[0].legs[0].distance.value/1000; 
costInput.value = (2 * (response.routes[0].legs[0].distance.value/1000)); 

      } 
     }); 




    } 
    </script> 
</head> 
<body onload="initialize()"> 
    <div> 

     <p> 
      <label for="start">Pickup from: </label> 
      <input type="text" name="start" id="start" autocomplete="on" size="40"/> 

      <label for="end">Delivered to: </label> 
      <input type="text" name="end" id="end" autocomplete="on" size="40"/><BR> 
Other Deliveries:<BR> 
<textarea id="waypoints" name="waypoints" onclick="this.value='';" cols="10" rows="10">Address, Suburb, State 
(Address is Optional) 
New line for each address</textarea> 
     </p> 
<p> 
<div id="ins"></div> 
</p> 
<p> 
      <input type="submit" value="Calculate" onclick="if (waypoints.value == 'Address, Suburb, State\n(Address is Optional)\nNew line for each address') { waypoints.value=''}; calcRoute()" /> 
     </p> 
     <p> 
      <label for="distance">Distance (km): </label> 
      <input type="text" name="distance" id="distance" readonly="true" /> 
     </p> 
     <p> 
      <label for="cost">MAX cost: 1 standard item going direct</label> 
      <input type="text" name="cost" id="cost" readonly="true"/> 
     </p> 
    </div> 
    <div id="map_canvas" style="width:300px; height:200px;"></div> 

我知道: distanceInput.value = response.routes [0] .legs [0] .distance.value/1000; costInput.value =(2 *(response.routes [0] .legs [0] .distance.value/1000)); 進行計算。但是我沒有把它們全部計算在一起。

謝謝

+0

的[添加航點谷歌距離矩陣來確定里程]重複(http://stackoverflow.com/questions/28702077/adding-waypoints-to- google-distance-matrix-to-determine-mileage) – geocodezip

回答

0

如果添加航點,也有response.routes [0] .legs超過1元。

所以距離應計算,因爲這

var totalDistance = 0; 

response.routes[0].legs.forEach(function(leg) { 
    totalDistance += leg.distance.value/1000; 
}) 

distanceInput.value = totalDistance; 
+0

WoW ..很棒!非常感謝 :) –