Navigator.geolocation.watchPosition returns only 100 m

I use navigator.geolocation.watchPosition in my own project to draw the path on the map while the user is moving. I noticed that the return frequency is pretty low for this function. I taught that it was a frequency, at least when I tested using the iOS emulator and the "freeway drive" mode in the gps emulator. Now, when I tested the "city mileage", I see that the frequency of position return does not depend on a certain time interval, but instead on a distance ... The function returns its position every 100 meters regardless of how long it took to the situation has changed a lot.

Why is that? Is this expected behavior? I don’t know if this is connected with the iOS emulator or with my code, but I would really like the position to be more accurate, I want it to return as often as possible.

componentDidMount() { const { region } = this.state; navigator.geolocation.getCurrentPosition( (position) => { this.setState({position}); }, (error) => alert(JSON.stringify(error)), {enableHighAccuracy: true, timeout: 20000, maximumAge: 1000} ); this.watchID = navigator.geolocation.watchPosition((lastPosition) => { var { distanceTotal, record } = this.state; this.setState({lastPosition}); if(record) { var newLatLng = {latitude:lastPosition.coords.latitude, longitude: lastPosition.coords.longitude}; this.setState({ track: this.state.track.concat([newLatLng]) }); this.setState({ distanceTotal: (distanceTotal + this.calcDistance(newLatLng)) }); this.setState({ prevLatLng: newLatLng }); } }, (error) => alert(JSON.stringify(error)), {enableHighAccuracy: true, timeout: 20000, maximumAge: 0}); } 
+6
source share
1 answer

There is an option that you can set called distanceFilter , which you set in meters. This is indicated in the documentation for geolocation , but it is not explained what it does or the default value. If you look at the github source code, then the default value is 100 , which explains your behavior.

If you need an accuracy of 1 meter, you specify the following parameters: {enableHighAccuracy: true, timeout: 20000, maximumAge: 0, distanceFilter: 1}

+15
source

Source: https://habr.com/ru/post/1013683/


All Articles