Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

W3C geolocation returns cached coordinates

I have a Chrome extension which uses the W3C Geolocation API to get the user's location. The extension is mostly used on Chromebooks.

I have noticed that the API seems to return the same coordinates (cached ?) most of the time, even though I have the maximumAge parameter set to 0. I tested this by deploying the extension on a Chromebook at location #1, then took the device to location #2, which is about 1 kilometer away. There the API was called again and it returned the same coordinates as before. A while later (I had it on a 10 minute interval) it returned the correct coordinates, but when I came back to location #1, I got cached coordinates again, ths time from location #2.

I tried with getCurrentPosition, watchPosition and several combinations of the options object, yet it always seems to produce the same outcome.

My current code is as follows:

function getGeolocation() {
  return new Promise((resolve, reject) => {
    const options = { enableHighAccuracy: true, timeout: 30000, maximumAge: 0 };
    navigator.geolocation.getCurrentPosition(resolve, reject, options);
  });
}

getGeolocation()
  .then(position => handler(position))
  .catch(err => console.error(err));

I need this to return the correct coordinates on first try or within a reasonable short time frame. Any advice?

Thanks!

EDIT:

To note that changing coordinates in the browser's sensors tabs works perfectly every time, but in a real situation it just seems to return cached results in most cases.

EDIT (Nov 5th):

After some more testing it seems that this occurs only on Chrome OS. We could not reproduce the same behavior on a PC (with or without an extension).

like image 350
rok Avatar asked Oct 25 '18 08:10

rok


People also ask

What does the W3C geolocation API do?

The W3C Geolocation API is an effort by the World Wide Web Consortium (W3C) to standardize an interface to retrieve the geographical location information for a client-side device.

What does geolocation getCurrentPosition return?

getCurrentPosition. Returns the device's current position as a [Position](Position/position.

What geographic coordinate reference system is supported by the Geolocation API?

The geographic coordinate reference system used by the attributes in this interface is the World Geodetic System (2d) [WGS84]. No other reference system is supported. The latitude and longitude attributes are geographic coordinates specified in decimal degrees.

What does showPosition () in Geolocation API returns?

Clarification: showPosition() method returns both latitude and longitude of user. Syntax is navigator. geolocation. getCurrentPosition(showPosition); The value of latitude and longitude returned will be in decimal.


1 Answers

Here is some info I gathered exploring W3C Geolocation API.

First, I ran the Wikipedia example code at the bottom of the page. It is almost same as your code, only you use Promise. From there, I got the position, latitude and longitude with method getCurrentPosition(). Method watchPosition() also worked.

I tested this on my PC and displayed results in Chrome Developers Console. It also worked on my mobile while walking around (last few digits were changing on latitude and longitude).

Regarding the PositionOptions interface, reading from your options, wait time that is allowed to pass from calling getCurrentPosition() or watchPosition() until successCallback is invoked, is 30000 milliseconds (30 seconds) and because maximumAge is 0, it won't use cached locations, instead, it must immediately attempt to acquire a new position object.

Now for the important part.


enableHighAccuracy provides a hint that the application would like to receive the best possible results. But there are ways it is determined (check wikipedia article at the beginning, scroll to "Location Sources"):

  1. GPS (Global Positioning System) - It has the highest accuracy; in most Android smartphones, the accuracy can be up to 10 metres.

  2. Mobile phone tracking - used if a cellphone or wireless modem is used without a GPS chip built in.

  3. WiFi Positioning System

  4. IP Address Location

There is also position accuracy, which gives an accuracy in meters. On my PC I got accuracy around 100 000 meters, so it is not valid latitude and longitude (search it on google maps, enter "latitute, longitude" and see where marker points at. Also, console.log(position.coords.accuracy) ). On my mobile, I got it around 15 - 50 meters.

Smartphone is using GPS location information, while PC uses network location. I'm not really into Chromebooks, but I found this answer:

No current Chromebook has the GPS feature. But you can let a site know your location. If you use Google as your default search engine on your phone, your location is used by default for your searches on Google. If you let Chrome share your location with a site, Chrome sends information to Google Location Services to get an estimate of where you are. Chrome can then share that info with the site that wants your location.

There are other ways to collect location, see Google Maps Geolocation API. Also, try to search diffrences between these two. Hope this helps in any way.

Best regards.

like image 105
Dinko Pehar Avatar answered Oct 17 '22 03:10

Dinko Pehar