Currently, my code is functional but lacks reliability. I often encounter delays and sometimes it doesn't update at all. My goal is to achieve real-time position updates. To accomplish this, I have utilized the setInterval()
function within the componentDidMount()
method.
I attempted to implement
navigator.geolocation.watchPosition
, but encountered issues. It's possible that I may not be using it correctly.
componentDidMount() {
this.state.interval = setInterval(() => {
navigator.geolocation.getCurrentPosition(
(position) => {
console.log("Pos: "+position.coords.speed);
this.setState({
latitude: position.coords.latitude,
longitude: position.coords.longitude,
speed: position.coords.speed,
error: null,
});
},
(error) => this.setState({ error: error.message }),
{ enableHighAccuracy: true, maximumAge: 0},
);
},1000);
}