Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
February 8, 2021 03:15 pm GMT

How to get an accurate position estimate from the Geolocation API in JavaScript

The Geolocation API has been introduced into modern browsers many years ago and hasn't changed much since yet it still can waste many hours of your time if you don't know how to work with it. There's a lot of magic happening behind the scenes that isn't properly explained in the documentation. Here's a straightforward way to get an accurate estimate without you having to spend the 2 days I've spent figuring out why my location estimates look like out of a random number generator.

There are 2 functions you can use to ask the browser for location estimates: getCurrentPosition() and watchPosition(). So far so good: the first will spit out 1 position on success, while the second will keep throwing new positions at you as they are updated. It's important to note that the GeolocationCoordinates object that we get as a result of either of these 2 functions contains the estimated position and accuracy of the measurement in meters.

For my application, where the user was supposed to trigger a location measurement it seemed obvious to use the getCurrentPosition() since in that case, I wouldn't have to take care of storing the state of change coming from watchPosition() and having to use clearWatch() to stop listening at an appropriate time. It seemed perfect. And turned out to be completely useless.

The getCurrentPosition() accepts an options object where you can turn enableHighAccuracy boolean to true. It comes with high hopes and an even larger disappointment. Even with the boolean, the measurements I'd get from my phone would have an accuracy of thousands of meters which rendered them virtually useless for what I needed.

Enter watchPosition(). After reading some obscure blog I don't remember the name of that went into details of how the GPS module might work in the phone, I learned that it might take a few seconds to warm up and spit out a correct position. And that is the crucial piece of knowledge you need to solve this problem. One that should definitely be explained in more depth in some of the official sources that explain how to use this API.

Knowing that I implemented my logic using watchPosition() instead and it turned out that indeed, magically the accuracy again starts at thousands of meters but, after a few seconds of these bad measurements, the GPS kicks in and provides estimates with a few meters of accuracy. These, finally, make sense for my application.

Here's an example of a function I use within the React's useEffect() hook. Note the returned function that allows me to clear the watch by returning it from the hook.

const readLocation = (  setLocation: (location: ILocation) => void,  setError: (errorMessage: string) => void,  setAccuracy: (acc: number) => void) => {  if (navigator.geolocation) {    const geoId = navigator.geolocation.watchPosition(      (position) => {        const lat = position.coords.latitude;        const lng = position.coords.longitude;        setLocation({ lat, lng });        setAccuracy(position.coords.accuracy);        console.log({ lat, lng }, position.coords.accuracy);        if (position.coords.accuracy > 10) {          showErrorSnackBar("The GPS accuracy isn't good enough");        }      },      (e) => {        showErrorSnackBar(e.message);        setError(e.message);      },      { enableHighAccuracy: true, maximumAge: 2000, timeout: 5000 }    );    return () => {      console.log('Clear watch called');      window.navigator.geolocation.clearWatch(geoId);    };  }  return;};
Enter fullscreen mode Exit fullscreen mode

That's all you need to get accurate estimates from the Geolocation API. Let me know in the comments if this worked for you


Original Link: https://dev.to/m3h0w/how-to-get-an-accurate-position-estimate-from-the-geolocation-api-in-javascript-1njf

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To