Reputation: 65
I have a big problem with expo-location package accuracy.
Based on this example https://github.com/byCedric/office-marathon I was trying to write an app tracking my car's position during drive.
Unfortunately, location coordinates are not accurate. From time to time coordinates show the wrong point hundreds of meters from my real position. I tried to fix it by including only coordinates with the best 'accuracy' parameter (accuracy < 10), however, it doesn't matter as still the coordinates can be very inaccurate.
I have all the required permissions on my Android device, and I have set accuracy as "Location.Accuracy.BestForNavigation".
I tried to change some settings regarding timaInterval
and distanceInterval
, but without any positive effect.
Could you tell me if I can do anything with that? Maybe it is completely normal behavior and in order to get more accurate coordinates I have to use a pay solution, do you have any knowledge about it?
Upvotes: 0
Views: 665
Reputation: 41
I have been using expo-location for a while now. It is true that the accuracy of the coordinates are usually poor. To achieve a better accuracy, of less than 10M, apart from just requesting location permissions, I usually employ the following:
Enable network provider
Location.enableNetworkProviderAsync();
On android phones, before using your application, make sure that Google Map's geolocation service has a better accuracy. You can do that by double-tapping on your location marker until the accuracy is good. Once you do that, your application's coordinates will have a better accuracy.
Continously capture coordinates until the best coordinate is captured. You can use the following structure:
const MAX_TRIES = 10;
const DELAY_IN_MS = 3000;
const captureLocation = async(idx) => {
let x = await Location.requestForegroundPermissionsAsync();
if (x.status === "granted") {
setCurrentBeacon(idx);
setGeolocating(true);
let tries = 0;
let bestLocation = null;
do {
try {
const loc = await Location.getCurrentPositionAsync({
accuracy: Location.Accuracy.Highest,
enableHighAccuracy: true,
timeout: DELAY_IN_MS, // Use timeout for individual requests
});
// Update the best location based on accuracy
if (
bestLocation === null ||
bestLocation.coords.accuracy > loc.coords.accuracy
) {
bestLocation = loc;
}
setLocation(bestLocation);
if (loc.coords.accuracy <= 5 || tries === MAX_TRIES - 1) {
setBestLocation(bestLocation)
setGeolocating(false);
break; // Exit the loop when desired accuracy is achieved
} else {
tries += 1;
}
} catch (err) {
console.error("Error fetching location:", err);
tries += 1;
}
} while (tries <= MAX_TRIES);
} else {
}
}
Upvotes: 1