'How should I obtain high accuracy using expo location?
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": some altitude,
"altitudeAccuracy": 41.362125396728516,
"heading": 230,
"latitude": not important,
"longitude": not important,
"speed": I dont know what is this for,
},
"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 say "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. I could not find any similar problems, or questions.
I am testing on Android, if that matters.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|