Usman Mutawakil
Usman Mutawakil

Reputation: 5259

Resetting browser geolocation values

Problem: My mobile JavaScript application kept returning the exact same coordinates, that were off by over 500 meters. The accuracy is not so much of the issue. What bothers me is the fact that the exact same coordinates were being returned. The problem was not solved till I visited an external web page that was also using geolocation. Since then, my application has functioned properly. I suspect somehow an incorrect location was cached for my page, and as soon as the browser detected a new site was requesting location then that value was flushed. This is just my theory.

Question(s): Does JavaScript cache location information? If so, is there any known means of forcing a refresh? The solution I described above is not practical outside of testing.

Code:

navigator.geolocation.getCurrentPosition(processLocation,locationError,{
                timeout: 60000,
     enableHighAccuracy: true,
             maximumAge: Infinity
});

function processLocation(position){ 
      sendLocationToServer(position.coords.latitude,position.coords.longitude);
}

Hardware: I'm using an iPhone and the safari web browser. I don't think using an iPhone should matter as much as the fact that I'm on safari but I did try resetting location and privacy settings for the phone. This did not solve the problem, just ensured the browser would prompt me for location permission again.

--Update---

It may actually be caused by the "enableHighAccuracy" field. Removing it has solved a second reoccurrence of the issue. When I find more info on why this parameter is having the opposite of it's intended effect I will post the details.

Upvotes: 1

Views: 2727

Answers (1)

Usman Mutawakil
Usman Mutawakil

Reputation: 5259

Clearing Location Cache: Set maximumAge to 0 if you do not want a cached value. I misinterpreted the maximumAge field. I confused it with timeout. It specifies how old of a cached location value should be utilized.

Inaccurate Location Values: As for the sporadic incorrect values, you need to use watchCurrentPosition which will recursively obtain the current location info then check against Position.coor.accuracy. Don't assume the first value returned will always be under your desired threshold.

Upvotes: 4

Related Questions