1

I am developing react-native for the first time and I cannot say that I fully understand the location API. Location API has values ​​from 1 to 6 for accuracy and I want to get the GPS accuracy value instantly and show a warning if the value is less than 4. How can I achieve this?

expo-location version: 13.0.4 Platform: IOS, android

OyeeRatio
  • 205
  • 3
  • 13
  • This should help https://stackoverflow.com/questions/55393271/react-native-how-to-auto-fetched-the-otp-in-the-textfield-from-the-mobile-sms-sh/56223148 – Dipansh Khandelwal Nov 29 '21 at 19:32

2 Answers2

4

When you call Location.getCurrentPositionAsync(options) the response object contains "accuracy" key which you can use to get value of accuracy.

Moreover if you need to add check for GPS accuracy it would be better to use Location.enableNetworkProviderAsync(), it asks the user to turn on high accuracy location mode.

Shabbir Haider
  • 158
  • 1
  • 10
  • Thank you Shabbir, So what exactly does this accuracy value represent? Because it does not return a reliability value from 1 to 6. – OyeeRatio Nov 30 '21 at 15:46
  • It's the maximum radius of un certainty in meters, smaller the value better the accuracy, – Shabbir Haider Dec 01 '21 at 05:31
  • @ShabbirHaider I think it is the opposite, higher is better: https://docs.expo.dev/versions/latest/sdk/location/#accuracy And it is an enum, not a meter value if I am not mistaken on what is referred here. – rablentain Jul 13 '22 at 06:10
0

Explaining how the lib and GPS works might help.

GPS have an internal location with an accuracy values (probable distance error), you can consider it as the blue ball on google maps, when you don't know where you are.

You can get the location using two functions:

  • Location.getCurrentPositionAsync(options) Tries to get your current location.
  • Location.getLastKnownPositionAsync(options) Uses a cached value for a faster response, but it might return a wrong location.

The Accuracy parameters is an enum that goes 1-6 and the docs are not very clear. Going deep into the code i found that it affects the power usage on Android. Only mapping to three levels, the levels details are on Android Official Docs.

private static int mapAccuracyToPriority(int accuracy) {
    switch (accuracy) {
      case LocationModule.ACCURACY_BEST_FOR_NAVIGATION:
      case LocationModule.ACCURACY_HIGHEST:
      case LocationModule.ACCURACY_HIGH:
        return LocationRequest.PRIORITY_HIGH_ACCURACY;
      case LocationModule.ACCURACY_BALANCED:
      case LocationModule.ACCURACY_LOW:
      default:
        return LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY;
      case LocationModule.ACCURACY_LOWEST:
        return LocationRequest.PRIORITY_LOW_POWER;
    }
  }

And finally! How to check the accuracy of your current location. By reading the Accuracy Attribute of the response, it gives you the probable error in meters.

Gustavo Garcia
  • 1,905
  • 1
  • 15
  • 27