diff --git a/public/index.html b/public/index.html index d1e4715..6377c51 100644 --- a/public/index.html +++ b/public/index.html @@ -27,10 +27,12 @@ + + diff --git a/src/pages/nearle/orders/createorder1.js b/src/pages/nearle/orders/createorder1.js index 0851456..1922cf2 100644 --- a/src/pages/nearle/orders/createorder1.js +++ b/src/pages/nearle/orders/createorder1.js @@ -483,7 +483,8 @@ const Createorder1 = () => { console.log('startPoint', startPoint); console.log('endPoint', endPoint); if (startPoint.latitude != 0 && startPoint.longitude != 0 && endPoint.latitude != 0 && endPoint.longitude != 0) { - getDistance(); + // getDistance(); + calculateDistance(startPoint, endPoint); } }, [startPoint, endPoint]); @@ -507,6 +508,72 @@ 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); + + // 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); + } + 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}`); + } + } catch (error) { + console.error('Error calculating distance:', error); + } + }; + useEffect(() => { if (timeslotarr[0]) { let arr = []; @@ -759,7 +826,7 @@ const Createorder1 = () => { applocationid: parseInt(localStorage.getItem('applocationid')), deliveryid: clientinfo.customerid, pickuplocationid: tenantinfo.deliverylocationid, - kms: distance + kms: distance.toString() } ]; @@ -828,7 +895,7 @@ const Createorder1 = () => { deliverytype: pickCust.customerid !== 0 || dropCust.customerid !== 0 ? 'B' : 'C', delivered: '', itemcount: 1, - kms: distance || 0, + kms: distance.toString() || 0, locationid: +tenanatLocoId, //main or branch moduleid: +tenant.moduleid, orderamount: +totalCharge.toFixed(2) || 0,