W3C geolocation returns cached coordinates
Asked Answered
F

1

6

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).

Fulmis answered 25/10, 2018 at 8:23 Comment(4)
What an interesting question! I'll try it out tomorrow during my daily commute to check how quickly geolocation API would react to me changing placesCambyses
Looks like I can't reproduce it, at least on my mac with chrome's console opened - I fetched coordinates at home (WiFi network #1) and in another place (WiFi network #2). The API timed out for ~3-4 minutes after I connected to network #2 (although connection was ok) but then it produced a correct result. 1) Can you re-check your results without using a chrome extension? 2) Are the results in location #1 and location #2 fully accurate (you can put the coordinates via comma into google.maps to see if it's ok)?Cambyses
We got the same behavior without a chrome extension as well.Fulmis
What kind of networks were used in both cases? Did your PC's coordinates have the same accuracy?Cambyses
R
1

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.

Rammer answered 2/11, 2018 at 16:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.