Please provide the following:
- SDK Version: 39…0.0
- Platforms(Android/iOS/web/all): Android
Hi,
I am developing an App that makes usage of Location Expo for getting a position. The issue is pointed to never gets a higher accuracy.
What am I doing?:
- Getting the curent position with higher accuracy
- Every 500 milliseconds try to get the current position again, expecting a better accuracy.
- Displaying the most recent accuracy value than changes every half second and a litle bit more.
- keeping the best accuracy among every try.
Please, I will apreciate if someone could test my code and tell me if the accuracy get lower than 10 meters in few tries.
Create project
expo init app-gps-test
cd app-gps-test
Install this component:
npm i --save react-native-circular-progress react-native-svg
Replace o create next Code:
./App.js
import { StatusBar } from 'expo-status-bar';
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import MyLocation from './src/screens/MyLocation'
export default function App() {
return (
<View style={styles.container}>
<MyLocation />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
./src/screens/MyLocation.js
import { StatusBar } from 'expo-status-bar';
import React, { Component } from 'react';
import { StyleSheet, Text, View, TouchableOpacity , Divider} from 'react-native';
import * as Location from 'expo-location';
import { AnimatedCircularProgress } from 'react-native-circular-progress';
const latitudeDelta = 0.005
const longitudeDelta = 0.003
export default class MyLocation extends React.Component {
constructor(props) {
super(props)
this.state = {
position : "Espere ...",
timeInterval : null,
latitude:null,
longitude:null,
regionInicial: {
latitude: 25.1948475,
longitude: 55.2682899,
latitudeDelta,
longitudeDelta,
},
fill: 0,
coordsBest: { latitude:null, longitude:null, accuracy:null },
coords: { latitude:null, longitude:null, accuracy:null },
}
}
idInterval = 0
async componentDidMount() {
let { status } = await Location.requestPermissionsAsync();
if (status !== 'granted') {
setErrorMsg('Permission to access location was denied');
}
this.startTimeInterval()
}
startTimeInterval = () => {
this.idInterval = setInterval(() => {
this.recalcularGPS()
}, 500);
}
recalcularGPS = async () => {
clearInterval(this.idInterval)
let location = await Location.getCurrentPositionAsync({enableHighAccuracy: true,accuracy: Location.Accuracy.higher , timeout : 5000});
const { latitude, longitude, accuracy } = location.coords
const { coordsBest } = this.state
await this.setState((previousState) => ({
position: accuracy.toFixed(2),
fill: accuracy.toFixed(1),
coords: location.coords,
coordsBest: coordsBest.accuracy==null?location.coords:(coordsBest.accuracy > accuracy?location.coords:coordsBest),
regionInicial: {
...previousState.regionInicial,
latitude: latitude,
longitude: longitude,
}
}))
this.startTimeInterval()
}
stopCalcular = async () =>{
clearInterval(this.idInterval)
}
componentWillUnmount(){
clearInterval(this.idInterval)
}
render() {
const { position, fill,coords, coordsBest } = this.state
console.log("coordsBest", coordsBest)
console.log(coords.latitude, coordsBest.latitude)
return(
<View style={styles.container}>
<TouchableOpacity
onPress={() => this.stopCalcular()}
>
<Text style={{fontSize:40,marginBottom: 20,color:"#F79F1F",textAlign:'center'}}>Precisión</Text>
</TouchableOpacity>
<AnimatedCircularProgress
size={250}
width={15}
fill={fill>100?100:fill}
tintColor="#EA2027"
backgroundColor="#12CBC455">
{
(fill) => (
<Text style={{fontSize: 30, fontWeight: 'bold'}}>
{ this.state.fill } Mts
</Text>
)
}
</AnimatedCircularProgress>
{coords &&
<View style={{paddingVertical:10}}>
<Text>Lat: {coords.latitude}</Text>
<Text>Lon: {coords.longitude}</Text>
</View>
}
{coordsBest &&
<View style={{paddingVertical:20}}>
<Text style={{fontWeight:"bold", fontSize:18}}>La mejor hasta ahora</Text>
<Text>Precisión: {coordsBest.accuracy}</Text>
<Text>Lat: {coordsBest.latitude}</Text>
<Text>Lon: {coordsBest.longitude}</Text>
</View>
}
</View>
)
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
ScreenShot
In my case the above code gives me almost all the time an accuracy over 15 meters and just under 10 meters with so many tries. And I expect getting a fast and acceptable accuracy.
Is this a normal result?
Thanks for any help!
Regards
Exel.