I am using GPS location in one of my app in react native. I need to track user location at some intervals. but I am getting different latitude and longitude. I have already set it for high accuracy. But it does not return the accurate location of the user. Also, I found GPS return different locations in IOS and Android. Please help me with how I can get exact location of the user.
I am facing this problem a while ago, this core geolocation api doesn't work perfectly on real devices even you using the HighAccuracy flag true, so here i have found some lib which you can use in order to get exact location
react-native-geolocation-service
Here is my code which I have used with above lib, have tested with both IOS and android Devices
import Geolocation from 'react-native-geolocation-service'
This function used to get current location of use
const getUserCorrectLocation = async () => {
await Geolocation.getCurrentPosition(
currentLocation => {
const { coords } = currentLocation
},
error => {
console.log(error, 'error')
},
{ enableHighAccuracy: true, timeout: 15000, maximumAge: 10000, distanceFilter: 0,
forceRequestLocation: true }
)}
Second function that I have used is to track user location when he start his journey
useEffect(() => {
const watchId = Geolocation.watchPosition(
position => {
const { coords } = position
},
error => {
console.log(error, 'coords')
},
{
enableHighAccuracy: true,
interval: 2000,
fastestInterval: 1000,
distanceFilter: 2
}
)
return () => Geolocation.clearWatch(watchId)
}},[])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With