Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

navigator.geolocation.watchPosition only return each 100 m

I am using navigator.geolocation.watchPosition in my react native project to paint a path on a map while the user is moving. I noticed that the return frequency is quite low for this function. I taught it was the frequency at least, when I tested using the iOS emulator and the "freeway drive" mode in the gps emulator. Now when I tested with "city run" instead, I can see that the return frequency of the position is not dependent on some time interval, but instead on a distance... The function is returning its position once each 100 meters, no matter how long it took for the position to change that much.

Why is it like this? Is this a expected behaviour? I don't know if it has to do with the iOS emulator or with my code, but I would really like the position to be more precise, 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});
} 
like image 747
rablentain Avatar asked Jan 01 '17 11:01

rablentain


People also ask

What does Navigator geolocation return?

The Navigator. geolocation read-only property returns a Geolocation object that gives Web content access to the location of the device. This allows a Web site or app to offer customized results based on the user's location.

How does geolocation watchPosition work?

The Geolocation method watchPosition() method is used to register a handler function that will be called automatically each time the position of the device changes. You can also, optionally, specify an error handling callback function.

Why is geolocation not working?

If you are facing problems with geolocation on your website running our WordPress Directory theme using Google Chrome browser, it's because of Google's decision to allow geolocation functionality only for websites that use SSL certificates.

What is maximum age geolocation?

maximumAge. A positive long value indicating the maximum age in milliseconds of a possible cached position that is acceptable to return. If set to 0 , it means that the device cannot use a cached position and must attempt to retrieve the real current position.


1 Answers

There's an option you can set that is called distanceFilter which you set the accuracy in meters. It's stated in the documentation for geolocation but not explained what it does or the default value. If you take a look at the source code at github the default is set to 100 meters, which explains your behavior.

If you want 1 meter accuracy you set the options as: {enableHighAccuracy: true, timeout: 20000, maximumAge: 0, distanceFilter: 1}

like image 200
Kimmen Avatar answered Oct 28 '22 13:10

Kimmen