简体   繁体   中英

How to round Google Maps Duration and Distance calculator

I have a small distance and duration calculator and I'm wondering how I would go about rounding up/down my final figures so for example, instead of a final duration of 7.433333333333334 mins or a final distance of 3.604 KMs. I'd get 7 minutes and 4 Kms.

Here is my current code:

 <html> <head> <meta name="viewport" content="initial-scale=1.0, user-scalable=no"/> <title>Distance Calculator</title> <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> <style type="text/css"> #map_canvas { height: 50%; width: 100%; } </style> <script type="text/javascript"> var directionDisplay; var directionsService = new google.maps.DirectionsService(); var map; function initialize() { directionsDisplay = new google.maps.DirectionsRenderer(); var perth = new google.maps.LatLng(-31.949766, 115.860928); var myOptions = { zoom:12, mapTypeId: google.maps.MapTypeId.ROADMAP, center: perth } 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; var distanceInput = document.getElementById("distance"); var durationInput = document.getElementById("duration"); var request = { origin:start, destination:end, 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; durationInput.value = response.routes[0].legs[0].duration.value / 60; } }); } function updateDue() { var total = parseInt(document.getElementById("odoend").value); var val2 = parseInt(document.getElementById("odostart").value); var val3 = parseInt(document.getElementById("distance").value); // to make sure that they are numbers if (!total) { total = 0; } if (!val2) { val2 = 0; } if (!val3) { val3 = 0; } var ansD = document.getElementById("kmused"); ansD.value = total - val2; var ansE = document.getElementById("kmpriv"); ansE.value = ansD.value - val3; } </script> </head> <body onload="initialize()"> <div> <p> <label for="start">Enter your current clients address:</label> <br> <input type="text" name="start" id="start" /> <p> <label for="end">Enter your next clients address: </label> <br> <input type="text" name="end" id="end" /> <p> <input type="submit" value="Calculate Route" onclick="calcRoute()" /> </p> <label for="odostart">Enter start odometer reading: </label> <br> <input type="text" name="odostart" id="odostart" onchange="updateDue()"> <p> <label for="odoend">Enter end odometer reading: </label> <br> <input type="text" name="odoend" id="odoend" onchange="updateDue()"> <p> <label for="kmused">Total KMs used</label><br> <input type="text" name="kmused" id="kmused"> <p> <label for="kmpriv">Private KMs used</label><br> <input type="text" name="kmpriv" id="kmpriv"> <p> <p> <label for="distance">Distance to next client (km): </label><br> <input type="text" name="distance" id="distance" onchange="updateDue()" /> <p> <label for="duration">Duration to next client (min): </label><br> <input type="text" name="duration" id="duration" onchange="updateDue()" /> </p> </div> <div id="map_canvas"></div> </body> </html> 

我相信您正在寻找的是Math.round()函数。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM