expo / expo

An open-source framework for making universal native apps with React. Expo runs on Android, iOS, and the web.
https://docs.expo.dev
MIT License
32.74k stars 5.21k forks source link

Using expo location with accuracy: Location.Accuracy.High gives location which is not accurate #17382

Closed sammy532 closed 1 year ago

sammy532 commented 2 years ago

Summary

I am using expo-location from here As per all the examples I read the following code should do the trick:

useEffect(() => {
    (async () => {
      let { status } = await Location.requestForegroundPermissionsAsync();
      if (status !== "granted") {
        setErrorMsg("Permission to access location was denied");
        return;
      }

      let location = await Location.getCurrentPositionAsync({
        enableHighAccuracy: true,
        accuracy: Location.Accuracy.High,
      });
      setLocation(location);
    })();
  }, []);

However, the location which is being fetched is really really not high. When I print it on the console I got:

location:  Object {
  "coords": Object {
    "accuracy": 41.36199951171875,
    "altitude": not important,
    "altitudeAccuracy": not important,
    "heading": not important,
    "latitude": not important,
    "longitude": not important,
    "speed": not important,
  },
  "mocked": false,
  "timestamp": not important,
}

As you can see location.coords.accuracy is quite high. As far as I understand, in order to get closer to my actual location I should make this number smaller.

What I notice is that if I refresh the page several times, my location is more accurate. So I wrote the following "improvement":

const [location, setLocation] = useState(null);
  const [errorMsg, setErrorMsg] = useState(null);
  const [calculateLocationAgain, setCalculateLocationAgain] = useState(true);

  useEffect(() => {
    (async () => {
      let { status } = await Location.requestForegroundPermissionsAsync();
      if (status !== "granted") {
        setErrorMsg("Permission to access location was denied");
        return;
      }

      let location = await Location.getCurrentPositionAsync({
        enableHighAccuracy: true,
        accuracy: Location.Accuracy.High,
      });
      setLocation(location);
      isAccuracyTooHigh(location);
    })();
  }, [calculateLocationAgain]);

function isAccuracyTooHigh(location) {
    if (!location) {
      setCalculateLocationAgain(!calculateLocationAgain);
    } else if (location.coords.accuracy > 10) {
      setCalculateLocationAgain(!calculateLocationAgain);
    }
  }

Basically, I keep fetching the location, until location.coords.accuracy <= 10. Which indeed, does work, it gives me high and precise location this time.

What is not cool about this solution is that I keep refreshing the page something like 40 times, which I find it unnecessary.

For sure, there must be some reason, why my location is not being fetched with high precision on the first place. Can someone advice?

Managed or bare workflow? If you have ios/ or android/ directories in your project, the answer is bare!

managed

What platform(s) does this occur on?

Android

SDK Version (managed workflow only)

5.2.0

Environment

expo-env-info 1.0.3 environment info: System: OS: Linux 4.15 Ubuntu 18.04.6 LTS (Bionic Beaver) Shell: 4.4.20 - /bin/bash Binaries: Node: 15.14.0 - ~/.nvm/versions/node/v15.14.0/bin/node npm: 7.7.6 - ~/.nvm/versions/node/v15.14.0/bin/npm npmPackages: expo: ^42.0.0 => 42.0.3 react: 16.13.1 => 16.13.1 react-dom: 16.13.1 => 16.13.1 react-native: 0.63.4 => 0.63.4 react-native-web: ^0.11.7 => 0.11.7 react-navigation: ^4.4.4 => 4.4.4 npmGlobalPackages: expo-cli: 5.2.0 Expo Workflow: managed

Reproducible demo

const [location, setLocation] = useState(null);
  const [errorMsg, setErrorMsg] = useState(null);

  useEffect(() => {
    (async () => {
      let { status } = await Location.requestForegroundPermissionsAsync();
      if (status !== "granted") {
        setErrorMsg("Permission to access location was denied");
        return;
      }

      let location = await Location.getCurrentPositionAsync({
        enableHighAccuracy: true,
        accuracy: Location.Accuracy.High,
      });
      setLocation(location);
    })();
  }, []);
zeeshanibnali commented 2 years ago

I am facing the same issue... any update guys?

github-actions[bot] commented 1 year ago

This issue is stale because it has been open for 60 days with no activity. If there is no activity in the next 7 days, the issue will be closed.

sammy532 commented 1 year ago

I still have this issue. If someone has fixed it, please, let me know

Mowinski commented 1 year ago

Bump :) I have a similar issue. I live in the big city (Wrocław) and outside the accuracy is above 30 in BestForNavigation accuracy mode.

alexkev commented 1 year ago

The accuracy "accuracy": 41.36199951171875" isn't a measurement of accuracy at all. Just by looking at this, it reads like a coordinate itself.

The link you provided stated that it is 1 is the lowest and 6 is the highest accuracy which is what you would want for something like google maps.

My results are:

{"coords":{"altitude":0,"altitudeAccuracy":-1,"latitude":39.785834,"accuracy":5,"longitude":-119.406417,"heading":-1,"speed":-1},"timestamp":1670879217716.303}

The -1 implies it could not get the information I believe. What I want is to get the altitude accuracy but I haven't been able to.

According to expo its, "The radius of uncertainty for the location, measured in meters. Can be null on Web if it's not available." https://docs.expo.dev/versions/latest/sdk/location/

expo-bot commented 1 year ago

Hi there! It looks like your issue requires a minimal reproducible example, but it is invalid or absent. Please prepare such an example and share it in a new issue.

The best way to get attention to your issue is to provide a clean and easy way for a developer to reproduce the issue on their own machine. Please do not provide your entire project, or a project with more code than is necessary to reproduce the issue.

A side benefit of going through the process of narrowing down the minimal amount of code needed to reproduce the issue is that you may get lucky and discover that the bug is due to a mistake in your application code that you can quickly fix on your own.

Resources