-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added live location tracking component
- Loading branch information
Showing
2 changed files
with
101 additions
and
7 deletions.
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
__app/component/LiveLocationTracking/LiveLocationTracking.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ( | ||
<div ref={directionMapRef} style={{ height: '50vh' }} /> | ||
); | ||
} | ||
|
||
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; |
7 changes: 0 additions & 7 deletions
7
__app/component/WIP-LiveLocationTracking/LiveLocationTracking.js
This file was deleted.
Oops, something went wrong.