2016-03-20 78 views
-1

我已經嘗試了很多方法仍然無法找出爲什麼這個計算沒有經過,我使用谷歌距離計算來計算距離,然後計算票價。任何幫助,將不勝感激。NaN錯誤代碼錯了什麼?

<table border="0" cellpadding="0" cellspacing="3"> 
 
<tr> 
 
    <td colspan="2"> 
 
     Source: 
 
     <input type="text" id="txtSource" value="heathrow terminal 5" style="width: 300px" /> 
 
     &nbsp; Destination: 
 
     <input type="text" id="txtDestination" value="bath road, tw6" style="width: 300px" /> 
 
     <br /> 
 
     
 

 

 

 
     <input type="button" id="calufare" runat="server" value="Get Route" onclick="GetRoute()"/> 
 
     <hr /> 
 
    </td> 
 
</tr> 
 
<tr> 
 
    <td colspan="2"> 
 
     <asp:Label ID="dvDistance" runat="server" Text=""></asp:Label> 
 
     <asp:Label ID="dvDuration" runat="server" Text=""></asp:Label> 
 
     <asp:TextBox ID="rate" runat="server" Text="2.5"></asp:TextBox> 
 
     <asp:Label ID="Fareid" runat="server" Text="Price£ "></asp:Label> 
 

 
     <script> 
 

 
      
 
     </script> 
 
    </td> 
 
</tr> 
 
<tr> 
 
    <td> 
 
     <div id="dvMap" style="width: 500px; height: 200px"> 
 
     </div> 
 
    </td> 
 
    <td> 
 
     <div id="dvPanel" style="width: 500px; height: 200px"> 
 
     </div> 
 
    </td> 
 
</tr> 
 
</table> 
 

 
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false&libraries=places"></script> 
 
<script type="text/javascript"> 
 
    var source, destination; 
 
    var directionsDisplay; 
 
    var directionsService = new google.maps.DirectionsService(); 
 
    google.maps.event.addDomListener(window, 'load', function() { 
 
     new google.maps.places.SearchBox(document.getElementById('txtSource')); 
 
     new google.maps.places.SearchBox(document.getElementById('txtDestination')); 
 
     directionsDisplay = new google.maps.DirectionsRenderer({ 'draggable': false }); 
 
    }); 
 

 
    function GetRoute() { 
 
     var heathrow = new google.maps.LatLng(51.4711620, -0.4523710); 
 
     var mapOptions = { 
 
      zoom: 7, 
 
      center: heathrow 
 
     }; 
 
     map = new google.maps.Map(document.getElementById('dvMap'), mapOptions); 
 
     directionsDisplay.setMap(map); 
 

 
     //*    FOR PRICE CALCULATION       * // 
 

 

 
    
 

 

 

 

 

 
     //*********DIRECTIONS AND ROUTE**********************// 
 
     source = document.getElementById("txtSource").value; 
 
     destination = document.getElementById("txtDestination").value; 
 

 
     var request = { 
 
      origin: source, 
 
      destination: destination, 
 
      travelMode: google.maps.TravelMode.DRIVING 
 
     }; 
 
     directionsService.route(request, function (response, status) { 
 
      if (status == google.maps.DirectionsStatus.OK) { 
 
       directionsDisplay.setDirections(response); 
 
      } 
 
     }); 
 

 
     //*********DISTANCE AND DURATION**********************// 
 
     var service = new google.maps.DistanceMatrixService(); 
 
     service.getDistanceMatrix({ 
 
      origins: [source], 
 
      destinations: [destination], 
 
      travelMode: google.maps.TravelMode.DRIVING, 
 
      unitSystem: google.maps.UnitSystem.METRIC, 
 
      avoidHighways: false, 
 
      avoidTolls: false 
 
     }, function (response, status) { 
 
      if (status == google.maps.DistanceMatrixStatus.OK && response.rows[0].elements[0].status != "ZERO_RESULTS") { 
 
       var distance = response.rows[0].elements[0].distance.text; 
 
       var duration = response.rows[0].elements[0].duration.text; 
 
       var dvDistance = document.getElementById("dvDistance"); 
 
       dvDistance.innerHTML = ""; 
 
       dvDistance.innerHTML += "Distance: " + distance + "<br />"; 
 
       var dvDuration = document.getElementById("dvDuration"); 
 
       dvDuration.innerHTML = ""; 
 
       dvDuration.innerHTML += "Duration:" + duration; 
 
       
 
       var distanceMeters = response.rows[0].elements[0].distance.value; 
 
       var rate = document.getElementById('Fareid').value; 
 

 
       rate=document.getElementById('Fareid').innerHTML = rate/1000 * distanceMeters; 
 

 
      } else { 
 
       alert("Unable to find the distance via road."); 
 
      } 
 
     }); 
 
    } 
 

 

 
</script>

+1

你的代碼放在文本「距離」的距離值之前。然後你的代碼再次獲取'.innerHTML'並將其傳遞給'parseInt()'。 – Pointy

+0

你認爲在哪裏需要改變? –

+1

爲什麼不使用'parseInt(distance)'而不是解析innerHTML? – Barmar

回答

2

此代碼沒有返回一個數字:

var rate = document.getElementById('rate').value; 

如果您使用率爲距離的數值,數值(如response.rows[0].elements[0].distance.value;這是距離以米爲單位),您將得到一個數字答案。

這給了我一個結果:

var distanceMeters = response.rows[0].elements[0].distance.value; 
rate = 2.5; 
document.getElementById('Fareid').innerHTML = rate * distanceMeters; 
+0

非常感謝。 –

+0

其工作正常,但我不知道爲什麼它不工作在運行代碼片段的stackoverflow –