From 1ed3a447d382e2efb57d4e91afd1486bc60f484b Mon Sep 17 00:00:00 2001 From: Himanshu Gupta Date: Tue, 19 Mar 2024 18:37:32 +0530 Subject: [PATCH] added live location tracking component --- .../LiveLocationTracking.js | 101 ++++++++++++++++++ .../LiveLocationTracking.js | 7 -- 2 files changed, 101 insertions(+), 7 deletions(-) create mode 100644 __app/component/LiveLocationTracking/LiveLocationTracking.js delete mode 100644 __app/component/WIP-LiveLocationTracking/LiveLocationTracking.js diff --git a/__app/component/LiveLocationTracking/LiveLocationTracking.js b/__app/component/LiveLocationTracking/LiveLocationTracking.js new file mode 100644 index 0000000..3d22d9f --- /dev/null +++ b/__app/component/LiveLocationTracking/LiveLocationTracking.js @@ -0,0 +1,101 @@ +import React, { useEffect, useRef } from 'react'; +import PropTypes from 'prop-types'; +import dependentService from '../services/dependentJsService'; + +function LiveLocationTracking({ + googleKey, + isProdKey, + zoom, + mapTypeControl, + originLatLng, + destinationLatLng, +}) { + const directionMapRef = useRef(); + let directionsService; + let directionsRenderer; + let watchID = null; + + const createMap = (userCurrenrLocation) => { + const googleMap = new google.maps.Map(directionMapRef.current, { + mapTypeControl, + center: userCurrenrLocation, + zoom, + }); + directionsRenderer.setMap(googleMap); + }; + + const plotDirection = (currentLocations) => { + if (directionsService && directionsService.route && directionsService.route.length) { + directionsService + .route({ + origin: currentLocations, + destination: destinationLatLng, + travelMode: google.maps.TravelMode.TWO_WHEELER, + }) + .then((response) => { + directionsRenderer.setDirections(response); + }).catch((e) => console.warn(`Directions request failed due to ${e}`)); + } + }; + // WALKING - bike + // TWO_WHEELER - Walking + // DRIVING - Car + const locationError = (error) => { + if (error) { + if (error.code === 1 && error.message === 'User denied Geolocation') { + console.warn('User denied Geolocation'); + } else { + console.warn('Location loading failed'); + } + } + }; + + useEffect(() => { + const googleMapUrl = `https://maps.googleapis.com/maps/api/js?${isProdKey ? 'client' : 'key'}=${googleKey}`; + dependentService(googleMapUrl, 'googleMapLocationAPI') + .then(async () => { + directionsService = new google.maps.DirectionsService(); + directionsRenderer = new google.maps.DirectionsRenderer(); + createMap(originLatLng); + + // Adding a watch when user cordinates changes to replot the direction + watchID = navigator.geolocation.watchPosition( + (newPosition) => { + const lat = newPosition.coords.latitude; + const lng = newPosition.coords.longitude; + console.log(lat, lng); + plotDirection({ lat, lng }); + }, + locationError(), + { enableHighAccuracy: true, timeout: 30000, maximumAge: 2000, distanceFilter: 100 }, + ); + }) + .catch((error) => { + console.warn(error); + }); + return () => { + if (watchID) { + navigator.geolocation.clearWatch(watchID); + } + }; + }, []); + + return ( +
+ ); +} + +LiveLocationTracking.propTypes = { + destinationLatLng: PropTypes.object.isRequired, + zoom: PropTypes.number, + mapTypeControl: PropTypes.bool, + googleKey: PropTypes.string.isRequired, + isProdKey: PropTypes.bool.isRequired, +}; + +LiveLocationTracking.defaultProps = { + zoom: 13, + mapTypeControl: false, +}; + +export default LiveLocationTracking; diff --git a/__app/component/WIP-LiveLocationTracking/LiveLocationTracking.js b/__app/component/WIP-LiveLocationTracking/LiveLocationTracking.js deleted file mode 100644 index 3a101e7..0000000 --- a/__app/component/WIP-LiveLocationTracking/LiveLocationTracking.js +++ /dev/null @@ -1,7 +0,0 @@ -import React from 'react'; - -export default function LiveLocationTracking() { - return ( -
LiveLocationTracking
- ); -}