2016-06-28 53 views
-1

我需要幫助解決我的問題的人,我想在總估計和每分鐘0.35分鐘後添加10%如何添加此腳本我試過var結果:pct/10;沒有做到這一點,有人能以上帝的名義幫助我解決這個問題,我真的很感激這一點。我如何添加%和每分鐘的功能與JavaScript

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true&libraries=places"></script> 
 
    <script> 
 
//--------------Settings-------------------------------- \t 
 
//ISO 3166-1 Alpha-2 country code, use http://en.wikipedia.org/wiki/ISO 3166-2_alpha-2#US 
 
var countrycode="US" 
 
//Rate per mil (2.00) 
 
var ratepermi=2.50; 
 
//Minimum fare (60) 
 
var minimum_fare=50; 
 
//Currrency Symbol 
 
var currencysymbol="$"; 
 
//Avoid motorways/highways? true/false 
 
var avoidHighways=false; 
 
//Avoid toll roads? true/false 
 
var avoidTolls=false; 
 
//Show summary? true/false 
 
var showsummary=false; 
 
//Disclaimer text 
 
var disclaimer="Please be aware this is only an estimated fare and the final price will be quoted on request" 
 
//----------End Settings-------------------------------- \t 
 
\t 
 
function initialize() 
 
{ 
 
\t var options = {componentRestrictions: {country: countrycode}}; 
 
\t var input = /** @type {HTMLInputElement} */(document.getElementById('inputfrom')); 
 
\t var searchBoxfrom = new google.maps.places.Autocomplete(input,options); 
 
\t var input = /** @type {HTMLInputElement} */(document.getElementById('inputto')); 
 
\t var searchBoxto = new google.maps.places.Autocomplete(input,options); 
 
} 
 

 
function ftn_estimate() 
 
{ 
 
\t if (document.getElementById('inputfrom').value!="" && document.getElementById('inputto').value!="") 
 
\t { 
 
\t \t var origin = document.getElementById('inputfrom').value; 
 
\t \t var destination = document.getElementById('inputto').value; 
 
\t \t 
 
\t \t var service = new google.maps.DistanceMatrixService(); 
 
\t \t service.getDistanceMatrix(
 
\t \t { 
 
\t \t \t origins: [origin], 
 
\t \t \t destinations: [destination], 
 
\t \t \t travelMode: google.maps.TravelMode.DRIVING, 
 
\t \t \t unitSystem: google.maps.UnitSystem.IMPERIAL, 
 
\t \t \t avoidHighways: avoidHighways, 
 
\t \t \t avoidTolls: avoidTolls, 
 
\t \t }, callback); \t 
 
\t } 
 
} 
 

 
function callback(response, status) { 
 
    if (status != google.maps.DistanceMatrixStatus.OK) { 
 
    alert('Error was: ' + status); 
 
    } else { 
 
    var origins = response.originAddresses; 
 
    var destinations = response.destinationAddresses; 
 

 
    for (var i = 0; i < origins.length; i++) { 
 
     var results = response.rows[i].elements; 
 
\t 
 
     for (var j = 0; j < results.length; j++) { 
 

 
\t \t if(showsummary) 
 
\t \t { 
 
\t \t \t document.getElementById('summary').innerHTML=origins[i] + ' to ' + destinations[j] + ': ' + results[j].distance.text + ' in '+ results[j].duration.text; 
 
\t \t \t document.getElementById('summary').innerHTML+="<br /><font color='red'>" + disclaimer + "</font>" 
 
\t \t } 
 
\t \t document.getElementById('distance').value=(results[j].distance.value/1609.34).toFixed(1); 
 
\t \t document.getElementById('time').value=(results[j].duration.value/60).toFixed(1); 
 
\t \t 
 
\t \t var calc_fare=(results[j].distance.value/1609.34)*ratepermi; 
 
\t \t 
 
\t \t if (calc_fare<minimum_fare) 
 
\t \t { 
 
\t \t \t calc_fare=minimum_fare; 
 
\t \t } \t 
 
\t \t document.getElementById('fare').value=currencysymbol+calc_fare.toFixed(2); 
 
     } 
 
    } 
 
    } 
 
} 
 
google.maps.event.addDomListener(window, 'load', initialize); 
 
</script> 
 

 
<link href="../../../../css/style.css" rel="stylesheet" type="text/css"> 
 
<center> 
 
     <div id="formbox"> 
 
     <h1 class="fare_title">FARE ESTIMATE</h1> 
 
       <div class="div1"> 
 
       <span class="ride">Ride in Style</span><img class="check_icon" src="../../../../check2.png" alt="check1"><span class="ride">Safe and Reliable</span><img class="check_icon" src="../../../../check2.png" alt="check1"><span class="ride">Fully Licensed</span><img class="check_icon" src="../../../../check2.png" alt="check1"/> 
 
       </div> 
 
     <input id="inputfrom" type="text" placeholder="From" style="width:400px" class="inputform"> 
 
     <h3 style="margin-top: 10px; color: #3F3F3F; text-transform: uppercase;">to</h3> 
 
     <input id="inputto" type="text" placeholder="To" style="width:400px" class="inputto"> 
 
     <br/> 
 
     <input type="button" onclick="ftn_estimate();" value="Estimate Fare" class="fare_button"> 
 
     <br/><br/> 
 
     <table class="resultsbox"> 
 
      <tr><td class="time_style">Time (mins)</td><td><input id="time" readonly type="text" placeholder="--"></td></tr> 
 
      <tr><td class="distance_style">Distance (mi)</td><td><input id="distance" readonly type="text" placeholder="--"></td></tr> 
 
      <tr><td class="fare_style">Estimated Fare</td><td><input id="fare" readonly type="text" placeholder="--"></td></tr> 
 
     </table> 
 
     
 
     <span id="summary"></span> 
 
    </div> 
 
</center>

+0

如果你只共享代碼的相關部分,並改寫你的問題更清楚這將是一件好事。如果我理解正確,你想爲該值添加10%的值......'variable + = 0.1 * variable' – Guillaume

+0

對不起,如果我沒有澄清我的問題。上面我添加的地方。 var ratepermi = 2.50; var minimum_fare = 50 ;.我想在總費用2.50英鎊+ 0.35 =總額後加10%的費用,然後我想再加上10%來給出估算的最終結果。希望我沒有迷惑你。 – Eden

回答

0

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true&libraries=places"></script> 
 
    <script> 
 
//--------------Settings-------------------------------- \t 
 
//ISO 3166-1 Alpha-2 country code, use http://en.wikipedia.org/wiki/ISO 3166-2_alpha-2#US 
 
var countrycode="US" 
 
//Rate per mil (2.00) 
 
var ratepermi=2.50; 
 
//Minimum fare (60) 
 
var minimum_fare=50; 
 
//Currrency Symbol 
 
var currencysymbol="$"; 
 
//Avoid motorways/highways? true/false 
 
var avoidHighways=false; 
 
//Avoid toll roads? true/false 
 
var avoidTolls=false; 
 
//Show summary? true/false 
 
var showsummary=false; 
 
//Disclaimer text 
 
var disclaimer="Please be aware this is only an estimated fare and the final price will be quoted on request" 
 

 
var rateperminute=0.35; 
 
var ratio=1.1; // Price will be 110% of the computed estimate 
 

 
//----------End Settings-------------------------------- \t 
 
\t 
 
function initialize() 
 
{ 
 
\t var options = {componentRestrictions: {country: countrycode}}; 
 
\t var input = /** @type {HTMLInputElement} */(document.getElementById('inputfrom')); 
 
\t var searchBoxfrom = new google.maps.places.Autocomplete(input,options); 
 
\t var input = /** @type {HTMLInputElement} */(document.getElementById('inputto')); 
 
\t var searchBoxto = new google.maps.places.Autocomplete(input,options); 
 
} 
 

 
function ftn_estimate() 
 
{ 
 
\t if (document.getElementById('inputfrom').value!="" && document.getElementById('inputto').value!="") 
 
\t { 
 
\t \t var origin = document.getElementById('inputfrom').value; 
 
\t \t var destination = document.getElementById('inputto').value; 
 
\t \t 
 
\t \t var service = new google.maps.DistanceMatrixService(); 
 
\t \t service.getDistanceMatrix(
 
\t \t { 
 
\t \t \t origins: [origin], 
 
\t \t \t destinations: [destination], 
 
\t \t \t travelMode: google.maps.TravelMode.DRIVING, 
 
\t \t \t unitSystem: google.maps.UnitSystem.IMPERIAL, 
 
\t \t \t avoidHighways: avoidHighways, 
 
\t \t \t avoidTolls: avoidTolls, 
 
\t \t }, callback); \t 
 
\t } 
 
} 
 

 
function callback(response, status) { 
 
    if (status != google.maps.DistanceMatrixStatus.OK) { 
 
    alert('Error was: ' + status); 
 
    } else { 
 
    var origins = response.originAddresses; 
 
    var destinations = response.destinationAddresses; 
 

 
    for (var i = 0; i < origins.length; i++) { 
 
     var results = response.rows[i].elements; 
 
\t 
 
     for (var j = 0; j < results.length; j++) { 
 

 
\t \t if(showsummary) 
 
\t \t { 
 
\t \t \t document.getElementById('summary').innerHTML=origins[i] + ' to ' + destinations[j] + ': ' + results[j].distance.text + ' in '+ results[j].duration.text; 
 
\t \t \t document.getElementById('summary').innerHTML+="<br /><font color='red'>" + disclaimer + "</font>" 
 
\t \t } 
 
\t \t document.getElementById('distance').value=(results[j].distance.value/1609.34).toFixed(1); 
 
\t \t document.getElementById('time').value=(results[j].duration.value/60).toFixed(1); 
 
\t \t 
 
\t \t var calc_fare_dist=(results[j].distance.value/1609.34)*ratepermi; 
 
\t \t var calc_fare_time=(results[j].duration.value/60)*rateperminute; 
 
\t \t // Provided you want to add 0.35 per minute to the "per mile price" 
 
\t \t var calc_fare = (calc_fare_dist + calc_fare_time)*ratio; 
 

 
\t \t 
 
\t \t if (calc_fare<minimum_fare) 
 
\t \t { 
 
\t \t \t calc_fare=minimum_fare; 
 
\t \t } \t 
 
\t \t document.getElementById('fare').value=currencysymbol+calc_fare.toFixed(2); 
 
     } 
 
    } 
 
    } 
 
} 
 
google.maps.event.addDomListener(window, 'load', initialize); 
 
</script> 
 

 
<link href="../../../../css/style.css" rel="stylesheet" type="text/css"> 
 
<center> 
 
     <div id="formbox"> 
 
     <h1 class="fare_title">FARE ESTIMATE</h1> 
 
       <div class="div1"> 
 
       <span class="ride">Ride in Style</span><img class="check_icon" src="../../../../check2.png" alt="check1"><span class="ride">Safe and Reliable</span><img class="check_icon" src="../../../../check2.png" alt="check1"><span class="ride">Fully Licensed</span><img class="check_icon" src="../../../../check2.png" alt="check1"/> 
 
       </div> 
 
     <input id="inputfrom" type="text" placeholder="From" style="width:400px" class="inputform"> 
 
     <h3 style="margin-top: 10px; color: #3F3F3F; text-transform: uppercase;">to</h3> 
 
     <input id="inputto" type="text" placeholder="To" style="width:400px" class="inputto"> 
 
     <br/> 
 
     <input type="button" onclick="ftn_estimate();" value="Estimate Fare" class="fare_button"> 
 
     <br/><br/> 
 
     <table class="resultsbox"> 
 
      <tr><td class="time_style">Time (mins)</td><td><input id="time" readonly type="text" placeholder="--"></td></tr> 
 
      <tr><td class="distance_style">Distance (mi)</td><td><input id="distance" readonly type="text" placeholder="--"></td></tr> 
 
      <tr><td class="fare_style">Estimated Fare</td><td><input id="fare" readonly type="text" placeholder="--"></td></tr> 
 
     </table> 
 
     
 
     <span id="summary"></span> 
 
    </div> 
 
</center>

+0

非常感謝你的工作。所以現在我想了解這個部分,所以我可以鎖定在我的頭上。那麼爲什麼在var showsummary = false後添加var rateperminute = 0.35; //免責聲明文字 var disclaimer =「請注意這只是一個估計的票價,最終價格將根據要求報價」如果它是否在速率不變之後有效?以及這個工作變化率如何= 1.1; //如果您不介意解釋謝謝,價格將爲計算估算值的110%。 – Eden

+0

另一件事TIME(MINS)它計算從A到B的總分鐘,我在哪裏將其更改爲Hours-Minutes。 – Eden

+0

您定義rateperminute的地方並不重要(只要您在使用它之前定義它)。 關於「ratio = 1.1」,如果你想給calc_fare增加10%,你實際上想要100%calc_fare + calc_fare的10%:你想要calc_fare的110%,所以你可以乘以110/100 = 1.1) – Faibbus