So I have a pretty simple bit of JS using the navigator.geolocation.getCurrentPosition jammy.
$(document).ready(function(){
$(\"#business-locate, #people-l
I have been having exactly the same problem, and finding almost no information online about it. Nothing at all in the books. Finally I found this sober query on stackoverflow and (ha!) it was the final impetus I needed to set up an account here.
And I have a partial answer, but alas not a complete one.
First of all, realise that the default timeout for getCurrentPosition is infinite(!). That means that your error handler will never be called if getCurrentPosition hangs somewhere on the back end.
To ensure that you get a timeout, add the optional third parameter to your call to getCurrentPosition, for example, if you want the user to wait no more than 10 seconds before giving them a clue what is happening, use:
navigator.geolocation.getCurrentPosition(successCallback,errorCallback,{timeout:10000});
Secondly, I have experienced quite different reliability in different contexts. Here at home, I get a callback within a second or two, although the accuracy is poor.
At work however, I experience quite bizarre variations in behavior: Geolocation works on some computers all the time (IE excepted, of course), others only work in chrome and safari but not firefox (gecko issue?), others work once, then subsequently fail - and the pattern changes from hour to hour, from day to day. Sometimes you have a 'lucky' computer, sometimes not. Perhaps slaughtering goats at full moon would help?
I have not been able to fathom this, but I suspect that the back end infrastructure is more uneven than advertised in the various gung-ho books and websites that are pushing this feature. I really wish that they would be a bit more straight about how flakey this feature is, and how important that timeout setting is, if you want your error handler to work properly.
I have been trying to teach this stuff to students today, and had the embarassing situation where my own computer (on the projector and several large screens) was failing silently, whereas about 80% of the students were getting a result almost instantly (using the exact same wireless network). It's very difficult to resolve these issues when my students are also making typos and other gaffes, and when my own pc is also failing.
Anyway, I hope this helps some of you guys. Thanks for the sanity check!
I have this problem in Mozilla. All time: Error: Unknown error acquiring position.
Now i'm using 47 Mozilla. I have tried everything, but all time this problem. BUT then i open about:config in my addsress bar, go geo.wifi.ui and changed it value to "https://location.services.mozilla.com/v1/geolocate?key=test". works!
If u have Position acquisition timed out error, try to increase timeout value:
var options = {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
};
navigator.geolocation.getCurrentPosition(success, error, options);
I have been having similar issues, and have been looking into the possibility that browsers have limits on how often getCurrentPosition can be called. It seems I can often get a location, but if i refresh the page right away it will time out. If I wait for a bit, I can usually get a location again. This usually happens with FF. In Chrome and Safari, I have not yet noticed getCurrentPosition timing out. Just a thought...
Although I cannot find any documentation to support this, it was a conclusion I came to after much testing. Perhaps someone else has has some info about that?
@brennanyoung's answer is great, but if you're wondering what to do in the failure case you could use an IP geolocation API such as https://ipinfo.io (which is my service). Here's an example:
function do_something(coords) {
// Do something with the coords here
// eg. show the user on a map, or customize
// the site contents somehow
}
navigator.geolocation.getCurrentPosition(function(position) {
do_something(position.coords);
},
function(failure) {
$.getJSON('https://ipinfo.io/geo', function(response) {
var loc = response.loc.split(',');
var coords = {
latitude: loc[0],
longitude: loc[1]
};
do_something(coords);
});
};
});
See https://ipinfo.io/developers/replacing-getcurrentposition for more details.
As of mid 2020, none of the answers here provides any explanation, just hacking or guessing.
As @Coderer points out before me, secure context (https) is required today, so on more and more devices geolocation doesn't work at all with plain http:
Secure context This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
The third parameter of getCurrentPosition()
(and watchPosition()
which is more suitable here) is PositionOptions object consisting of these properties:
enableHighAccurancy
(default false): if set to true, response is slower and more accurate. If you got timeout errors, keep this to false. If the accurancy is low, set it to true. In my tests 1s delay with 1-2 meters precision and there was no difference between true and false on UMAX tablet. I tested also on older iPhone with 5 meters oscilation if set to false. You can also measure accurancy in GeolocationCoordinates.accurancy property and decide dynamically.timeout
(default infinity): milliseconds before the API gives up and calls the error handler (the second parameter). Today, 5s (5000) is reasonable and in some cases 10s can make sense. Useful if you want to have plan B when geolocation sensor data is not available.maximumAge
(default 0): milliseconds when cached value is valid, the device may decide to use valid cached data instead of sensor measure. Set this to Infinity on static locations, keep at 0 otherwise.As @YoLoCo points out before me, getCurrentPosition()
and watchPosition()
interferes and I confirm his results in 2020. Generally, use watchPosition instead of getCurrentPosition periodical calls.
This library adds a desiredAccuracy and maxWait option to geolocation calls, which means it will keep trying to get a position until the accuracy is within a specified range.