Что я пытаюсь сделать, это добавить минимальную стоимость 40, чтобы показать, не стоит ли рассчитанная стоимость ниже, но действительно изо всех сил пытается получить оператор IF, какие -либо идеи? < /p>
Код: Выделить всё
document.addEventListener('DOMContentLoaded', function() {
let map;
let directionsService;
let directionsRenderer;
function initMap() {
// Initialize the Google Maps objects
directionsService = new google.maps.DirectionsService();
directionsRenderer = new google.maps.DirectionsRenderer();
map = new google.maps.Map(document.getElementById('map'), {
center: { lat: -34.397, lng: 150.644 },
zoom: 8
});
directionsRenderer.setMap(map);
}
function calculateDistance() {
const pickup = document.getElementById('pickup').value;
const destination = document.getElementById('destination').value;
if (pickup && destination) {
const request = {
origin: pickup,
destination: destination,
travelMode: 'DRIVING'
};
directionsService.route(request, function(result, status) {
if (status == 'OK') {
directionsRenderer.setDirections(result);
const distance = result.routes[0].legs[0].distance.value / 1000; // distance in km
const cost = distance * 2; // Example: $2 per km
document.getElementById('distance').textContent = distance.toFixed(2) + ' km';
document.getElementById('total-cost').textContent = '$' + cost.toFixed(2);
} else {
alert('Could not calculate distance: ' + status);
}
});
} else {
alert('Please enter both pickup and destination locations.');
}
}
document.getElementById('calculate-button').addEventListener('click', calculateDistance);
// Load the map
initMap();
});
Подробнее здесь: https://stackoverflow.com/questions/795 ... -wordpress