Mahdi Hasan
Mahdi Hasan

Reputation: 11

react native geolocation for android returning cached position

I was using Geolocation from 'react-native-geolocation-service' library and the details are as follows. My mobile app is heavily depended on location (geo coordinates) hence getting the lat/lon correctly is a must. The problem I am facing is that android is returning the cached position. I have removed the maximumAge parameter in my code but still I am getting the cached lat long in android. Is there a way that I can always get the current location from the GPS in android in react native using this library? Any help is appreciated. Thanks.

AndoidManifest.xml:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />


Location.js:
import Geolocation from 'react-native-geolocation-service';

  getCoordinates = () => {
    Geolocation.getCurrentPosition(
      position => {
        this.setState({
          latitude: position.coords.latitude,
          longitude: position.coords.longitude,
        });
      },
      error => {
      }, { enableHighAccuracy: true, timeout: 15000 },
    )
  };

Upvotes: 1

Views: 1715

Answers (1)

Kishan Bharda
Kishan Bharda

Reputation: 5690

If you don't provide any parameter then it will use the default value for that parameter. So, In this case, you removed maximumAge parameter, which default value is INFINITY as per this doc.

So, try passing maximumAge: 0 in option and it will provide newly cordinates like this :

getCoordinates = () => {
    Geolocation.getCurrentPosition(
      position => {
        this.setState({
          latitude: position.coords.latitude,
          longitude: position.coords.longitude,
        });
      },
      error => {
      }, { enableHighAccuracy: true, timeout: 15000, maximumAge: 0 },
    )
  };

Upvotes: 5

Related Questions