Code: Select all
Route Miles Only
body { font-family: sans-serif; padding: 20px; }
input { padding: 8px; width: 200px; font-weight: bold; }
Total Route Distance:
function initMap() {
const directionsService = new google.maps.DirectionsService();
// Define your Start and End points
const request = {
origin: 'Nashville, TN',
destination: 'atlanta, ga',
travelMode: 'DRIVING',
unitSystem: google.maps.UnitSystem.IMPERIAL // Ensures results are in miles
};
directionsService.route(request, (result, status) => {
if (status === 'OK') {
// Extract the distance string (e.g., "473 mi")
const miles = result.routes[0].legs[0].distance.text;
// Display it in the text field
document.getElementById('distance-field').value = miles;
} else {
document.getElementById('distance-field').value = "Error";
}
});
}
// Initialize the script
window.onload = initMap;
Mobile version