Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GPS location fluctuating in react native

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.

like image 600
Dayachand Patel Avatar asked Feb 03 '26 02:02

Dayachand Patel


1 Answers

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)
}},[])
like image 80
ashutosh pandey Avatar answered Feb 04 '26 16:02

ashutosh pandey