updates on the google change
This commit is contained in:
@@ -34,6 +34,7 @@ import {
|
||||
FormControlLabel
|
||||
} from '@mui/material';
|
||||
import CloseIcon from '@mui/icons-material/Close';
|
||||
import { calculateDrivingDistance, calculateTotalCharge } from '../../../utils/distance';
|
||||
import { Empty } from 'antd';
|
||||
import { FaPhoneAlt } from 'react-icons/fa';
|
||||
import { GiDoorHandle } from 'react-icons/gi';
|
||||
@@ -619,66 +620,25 @@ const Createorder1 = () => {
|
||||
};
|
||||
|
||||
const calculateDistance = async (pickup, drop) => {
|
||||
const service = new google.maps.DistanceMatrixService();
|
||||
|
||||
const getDistanceMatrix = (origins, destinations, travelMode, unitSystem) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
service.getDistanceMatrix(
|
||||
{
|
||||
origins: [new google.maps.LatLng(origins.latitude, origins.longitude)],
|
||||
destinations: [new google.maps.LatLng(destinations.latitude, destinations.longitude)],
|
||||
travelMode: travelMode,
|
||||
unitSystem: unitSystem
|
||||
},
|
||||
(response, status) => {
|
||||
if (status === 'OK') {
|
||||
resolve(response);
|
||||
} else {
|
||||
reject(new Error(`Error calculating distance: ${status}`));
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
try {
|
||||
// Use await to wait for the promise to resolve
|
||||
const response = await getDistanceMatrix(pickup, drop, 'DRIVING', google.maps.UnitSystem.METRIC);
|
||||
const roundedDistance = await calculateDrivingDistance(pickup, drop);
|
||||
console.log('calculated distance in km:', roundedDistance);
|
||||
setDistance(roundedDistance);
|
||||
|
||||
// Handle the response
|
||||
const results = response.rows[0].elements;
|
||||
for (let i = 0; i < results.length; i++) {
|
||||
const element = results[i];
|
||||
const distance = element.distance.value;
|
||||
console.log('distance in m ', distance);
|
||||
const distanceInKm = (distance / 1000).toFixed(2);
|
||||
console.log('distance in km ', distanceInKm);
|
||||
const roundedDistance = Math.round(distanceInKm);
|
||||
console.log('roundedDistance', roundedDistance);
|
||||
setDistance(roundedDistance);
|
||||
if (roundedDistance < minKm) {
|
||||
setTotalCharge(basePrice);
|
||||
} else {
|
||||
console.log('minKm', minKm);
|
||||
console.log('pricePerKm', pricePerKm);
|
||||
console.log('basePrice', basePrice);
|
||||
const total = (roundedDistance - minKm) * pricePerKm + basePrice;
|
||||
console.log('total', total);
|
||||
setTotalCharge(total);
|
||||
}
|
||||
const total = calculateTotalCharge(roundedDistance, basePrice, pricePerKm, minKm);
|
||||
console.log('total charge:', total);
|
||||
setTotalCharge(total);
|
||||
|
||||
setShowDistance(true);
|
||||
if (roundedDistance > appLocaRadius) {
|
||||
setShowDistance(true);
|
||||
if (roundedDistance > appLocaRadius) {
|
||||
setShowDistance(true);
|
||||
setOpen4(true);
|
||||
}
|
||||
|
||||
// Extract the numerical value of the duration
|
||||
const durationMatch = element.duration.text.match(/([\d.]+)/);
|
||||
const duration = durationMatch ? parseInt(durationMatch[0]) : null;
|
||||
|
||||
// Display only the numerical values
|
||||
console.log(`Distance: ${roundedDistance}, Duration: ${duration}`);
|
||||
setOpen4(true);
|
||||
}
|
||||
|
||||
// ⏱️ Approximate duration
|
||||
const avgSpeed = 40; // km/h (adjust if needed)
|
||||
const duration = Math.round((roundedDistance / avgSpeed) * 60); // minutes
|
||||
console.log(`Distance: ${roundedDistance}, Duration: ${duration}`);
|
||||
} catch (error) {
|
||||
console.error('Error calculating distance:', error);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user