Reputation: 43
have you done any live tracking demo like Uber in react native?
i made one. it's working fine in emulator & simulator with custom location but couldn't get proper location in real device using navigator.geolocation.
watchID = null;
componentDidMount(){
navigator.geolocation.getCurrentPosition((position) => {
let lat = parseFloat(position.coords.latitude);
let long = parseFloat(position.coords.longitude);
let initialRegion = {
latitude : lat,
longitude : long,
latitudeDelta : LATITUDE_DELTA,
longitudeDelta : LONGITUDE_DELTA
}
this.setState({ initialPosition : initialRegion });
this.setState({ markerPostition : initialRegion });
}, (error) => {
alert(JSON.stringify(error))
});
this.watchID = navigator.geolocation.watchPosition((position) => {
let lat = parseFloat(position.coords.latitude);
let long = parseFloat(position.coords.longitude);
let lastRegion = {
latitude : lat,
longitude : long,
latitudeDelta : LATITUDE_DELTA,
longitudeDelta : LONGITUDE_DELTA
}
this.setState({ initialPosition : lastRegion });
this.setState({ markerPostition : lastRegion });
});
}
componentWillUnmount(){
navigator.geolocation.clearWatch(this.watchID);
}
i had set enableHighAccuracy to true but it was throwing error, so, i just removed it. now i'm not getting proper (exact) location of device.
please suggest me Geolocation setup with watchPosition what's worked better for you in case of you made any related project(s).
if you have your code in git repo then i will be thankful of your.
forgive me if i did any grammatical mistakes.
Upvotes: 1
Views: 3063
Reputation: 87
componentDidMount() {
Geolocation.getCurrentPosition(
(pos) => {
const coords = {
latitude: pos.coords.latitude,
longitude: pos.coords.longitude
};
this.setState((prevState) => {
return {
focusedLocation: {
...prevState.focusedLocation,
latitude: coords.latitude,
longitude: coords.longitude
}
};
});
console.log(coords.latitude, coords.longitude);
return this.geocoder(coords.latitude, coords.longitude);
},
(err) => {
alert('Fetching the Position failed, please check location is enable!');
},
{ enableHighAccuracy: false, timeout: 10000 }
);
}
this will work perfectly...try this
Upvotes: 1
Reputation: 671
enableHighAccuracy: true which will help to find the exact location
Upvotes: 0
Reputation: 509
I have used geolocation in my project and it works perfectly, here is the setup of my geolocation try if it fixes your issue
navigator.geolocation.getCurrentPosition(
(position) => {
//position.coords.latitude for latitude
//position.coords.longitude for longitude
},
(error) => console.warn(error.message),
{ enableHighAccuracy: false, timeout: 10000 }
)
Watchposition code is same as yours
Upvotes: 0