updates on the changes google.maps.DistanceMatrixService to orsm
This commit is contained in:
@@ -7,6 +7,7 @@ import { useNavigate } from 'react-router';
|
||||
import { useTheme } from '@mui/material/styles';
|
||||
import useMediaQuery from '@mui/material/useMediaQuery';
|
||||
import { enqueueSnackbar } from 'notistack';
|
||||
import { calculateDrivingDistance, calculateTotalCharge } from '../../../utils/distance';
|
||||
|
||||
import {
|
||||
FormControl,
|
||||
@@ -296,53 +297,17 @@ const MultipleOrders = () => {
|
||||
settotalCash(a4);
|
||||
}, [dropCust]);
|
||||
|
||||
// ============================== distance (Google) ==============================
|
||||
// ============================== distance (OSRM/Haversine) ==============================
|
||||
const calculateDistance = async (customer) => {
|
||||
if (typeof window === 'undefined' || !window.google?.maps?.DistanceMatrixService) {
|
||||
throw new Error('Google Maps not loaded');
|
||||
}
|
||||
const service = new window.google.maps.DistanceMatrixService();
|
||||
|
||||
const getDistanceMatrix = (origins, destinations) =>
|
||||
new Promise((resolve, reject) => {
|
||||
try {
|
||||
if (!origins || !destinations) return reject(new Error('Origin or destination data missing.'));
|
||||
service.getDistanceMatrix(
|
||||
{
|
||||
origins: [new window.google.maps.LatLng(origins.latitude, origins.longitude)],
|
||||
destinations: [new window.google.maps.LatLng(destinations.latitude, destinations.longitude)],
|
||||
travelMode: 'DRIVING',
|
||||
unitSystem: window.google.maps.UnitSystem.METRIC
|
||||
},
|
||||
(response, status) => {
|
||||
if (status === 'OK') resolve(response);
|
||||
else reject(new Error(`Google API error: ${status}`));
|
||||
}
|
||||
);
|
||||
} catch (err) {
|
||||
reject(new Error(`Unexpected error inside DistanceMatrixService: ${err.message}`));
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
if (!customer || typeof customer !== 'object') throw new Error('Invalid customer data.');
|
||||
if (!pickCust || typeof pickCust !== 'object') throw new Error('Origin (pickCust) data missing or invalid.');
|
||||
|
||||
const response = await getDistanceMatrix(pickCust, customer);
|
||||
const distVal = response?.rows?.[0]?.elements?.[0]?.distance?.value;
|
||||
if (distVal == null) throw new Error('Malformed Distance Matrix response: missing distance value.');
|
||||
|
||||
const km = distVal / 1000;
|
||||
const roundedDistance = Math.round(km);
|
||||
const totalcharge =
|
||||
roundedDistance < minKm ? basePrice : (roundedDistance - minKm) * pricePerKm + basePrice;
|
||||
const roundedDistance = await calculateDrivingDistance(pickCust, customer);
|
||||
const totalcharge = calculateTotalCharge(roundedDistance, basePrice, pricePerKm, minKm);
|
||||
return { roundedDistance, totalcharge };
|
||||
} catch (error) {
|
||||
if (error.message.includes('Google API')) {
|
||||
OpenToast('Invalid coordinates or Google API error.', 'error', 3000);
|
||||
} else if (error.message.includes('Malformed Distance Matrix')) {
|
||||
OpenToast('Google Distance Matrix returned invalid data.', 'error', 3000);
|
||||
} else if (error.message.includes('Origin') || error.message.includes('customer')) {
|
||||
if (error.message.includes('Origin') || error.message.includes('customer') || error.message.includes('coordinates')) {
|
||||
OpenToast('Missing or invalid input data for distance calculation.', 'warning', 3000);
|
||||
} else {
|
||||
OpenToast('Unexpected error during distance calculation.', 'error', 3000);
|
||||
|
||||
Reference in New Issue
Block a user