1

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 },
    )
  };

1 Answer 1

5
+50

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 },
    )
  };
Sign up to request clarification or add additional context in comments.

1 Comment

I'll try this and give feedback, I was having the same issue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.