9

I'm struggling to use the maximumAge parameter in HTML geolocation. The way it works in Chrome does not seem to match the spec.

I've read through the geolocation API spec and from what I can understand, if you set the maximumAge to zero, you should always get a fresh position.

The maximumAge attribute indicates that the application is willing to accept a cached position whose age is no greater than the specified time in milliseconds. If maximumAge is set to 0, the implementation must immediately attempt to acquire a new position object. Setting the maximumAge to Infinity must determine the implementation to return a cached position regardless of its age. If an implementation does not have a cached position available whose age is no greater than the specified maximumAge, then it must acquire a new position object. In case of a watchPosition(), the maximumAge refers to the first position object returned by the implementation.

However, this is my code:

function gpsSuccess(pos) {
   var positionDate = new Date(pos.timestamp);
   var currentDate = new Date();
   console.log(positionDate.toLocaleString(), currentDate.toLocaleString());
}
watchId = navigator.geolocation.watchPosition(gpsSuccess,
    gpsFail, {
        enableHighAccuracy: true,
        maximumAge: 0,
        timeout: 27000
});

And I'm seeing the following:

Wed Nov 23 2011 10:25:43 GMT+0000 (GMT), Wed Nov 23 2011 10:52:16 GMT+0000 (GMT)

So that's half an hour's difference between the position obtained and the current time.

I thought it might just be the difference between the satellite time and the local time, but I've been hitting refresh repeatedly, and the positionDate isn't changing, even though the currentDate is.

Why is the same, cached position being reused, even though maximumAge is set to zero?

This is in Chrome, BTW.

Richard
  • 31,629
  • 29
  • 108
  • 145
  • I've been noticing similar behaviour on Safari-mobile too. Very frustrating. I wish I knew a way to force a fresh geo-lookup when I need it. I thought, like you did, that setting maximumAge to zero would do this (as the spec explains). Guess not... – robmclarty May 17 '13 at 17:14
  • Seems like there may simply be no control over this: http://stackoverflow.com/questions/14814256/javascript-geolocation-caching – robmclarty May 17 '13 at 17:20
  • Depending on your use case this issue can be very annoying. I get around this by setting a flag and ignoring the first position object returned in the success callback. – bardu Dec 11 '13 at 18:41
  • Facing same problem, getting location(Cached) even location services of the device are turned off. – Mandeep Pasbola Apr 08 '14 at 08:27

1 Answers1

0

This not possible. Same thing happen with some html5 tags such as webkit speech, that you can't activate through code. You don't have much control over the thing, sorry.

Valentin Roudge
  • 555
  • 4
  • 14