updates on the changes google.maps.DistanceMatrixService to orsm
This commit is contained in:
@@ -26,6 +26,7 @@ import {
|
||||
useMediaQuery
|
||||
} from '@mui/material';
|
||||
import CloseIcon from '@mui/icons-material/Close';
|
||||
import { calculateDrivingDistance, calculateTotalCharge } from '../../../utils/distance';
|
||||
import { Empty } from 'antd';
|
||||
import { FaPhoneAlt, FaBox, FaBoxes, FaTruck, FaArrowRight, FaArrowLeft, FaCheck, FaRoute, FaMoneyBillWave, FaChartLine, FaReceipt, FaPaperPlane } from 'react-icons/fa';
|
||||
import { GiDoorHandle } from 'react-icons/gi';
|
||||
@@ -365,130 +366,25 @@ const Createorder1 = () => {
|
||||
}, [startPoint, endPoint]);
|
||||
|
||||
// google distance matrix logic
|
||||
/*
|
||||
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);
|
||||
|
||||
// Handle the response
|
||||
const results = response.rows[0].elements;
|
||||
for (let i = 0; i < results.length; i++) {
|
||||
const element = results[i];
|
||||
|
||||
// Extract the numerical value of the distance
|
||||
|
||||
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);
|
||||
}
|
||||
setShowDistance(true);
|
||||
if (roundedDistance > appLocaRadius) {
|
||||
setShowDistance(true);
|
||||
setOpen(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}`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error calculating distance:', error);
|
||||
}
|
||||
};*/
|
||||
// Haversine + 1.3
|
||||
const calculateDistance = async (pickup, drop) => {
|
||||
// Haversine formula
|
||||
const haversineDistance = (lat1, lon1, lat2, lon2) => {
|
||||
const toRad = (value) => (value * Math.PI) / 180;
|
||||
|
||||
const R = 6371; // Earth radius in KM
|
||||
const dLat = toRad(lat2 - lat1);
|
||||
const dLon = toRad(lon2 - lon1);
|
||||
|
||||
const a =
|
||||
Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) * Math.sin(dLon / 2) * Math.sin(dLon / 2);
|
||||
|
||||
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
||||
return R * c; // distance in KM
|
||||
};
|
||||
|
||||
try {
|
||||
// 1. Get aerial distance
|
||||
const aerialDistance = haversineDistance(pickup.latitude, pickup.longitude, drop.latitude, drop.longitude);
|
||||
|
||||
// 2. Convert to road approximation (1.3x)
|
||||
const distanceInKm = (aerialDistance * 1.3).toFixed(2);
|
||||
|
||||
console.log('distance in km ', distanceInKm);
|
||||
|
||||
const roundedDistance = Math.round(distanceInKm);
|
||||
console.log('roundedDistance', roundedDistance);
|
||||
|
||||
// 🔽 SAME AS YOUR EXISTING LOGIC (UNCHANGED)
|
||||
const roundedDistance = await calculateDrivingDistance(pickup, drop);
|
||||
console.log('calculated distance in km:', 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);
|
||||
setOpen(true);
|
||||
}
|
||||
|
||||
// ⏱️ Approximate duration (optional, since no API)
|
||||
// ⏱️ 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