0

Take a look at this code:

navigator.geolocation.getCurrentPosition(function(){
    console.log("a");
});
navigator.geolocation.getCurrentPosition(function(){
    console.log("b");
});

https://jsfiddle.net/DerekL/sxb3j2bv/

After the permission is granted by the user, I would expect the console to have logged

> "a"
> "b"

and indeed this is what happened in Chrome. However on Firefox, for some reason it only fires once and only logs "b":

> "b"

What can I do about it? Is that a bug?

Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
  • In chrome, you must have got `"b"` and then `"a"` – Rayon Apr 07 '16 at 09:35
  • My assumption is second `navigator.geolocation.getCurrentPosition` is invoked too and it overrides initial permission prompt. As you allow second prompt, you get `"b"` in the console. As per the docs, _Be aware that each browser has its own policies and methods for requesting this permission._ [[Ref](https://developer.mozilla.org/en-US/docs/Web/API/Navigator/geolocation)] – Rayon Apr 07 '16 at 09:38
  • @RayonDabre You are right. `"b"` comes before `"a"`. – Derek 朕會功夫 Apr 07 '16 at 09:40
  • Possible dupe: http://stackoverflow.com/questions/3397585/navigator-geolocation-getcurrentposition-sometimes-works-sometimes-doesnt – Rayon Apr 07 '16 at 09:45

2 Answers2

0

I believe this is happening because the second call executes before the user has accepted the location permission for the first.

However, you probably want to look at

navigator.geolocation.watchPosition

rather than making multiple requests to get the position.

Tom Mettam
  • 2,903
  • 1
  • 27
  • 38
  • I'm not trying to make multiple calls to constantly "update" my internal data. The reason why my code makes multiple `getCurrentPosition` calls is because there are different modules in the page that requires geolocation, resulting in multiple calls to `getCurrentPosition`. – Derek 朕會功夫 Apr 07 '16 at 09:42
  • If you don't need an updated location, then just call getCurrentPosition once and then store the returned value, and use the value elsewhere. – Tom Mettam Apr 07 '16 at 09:43
0

I solved it by reusing the previous request's promise:

// Note that this is using AngularJS
.service("demo_geolocation_service", function($q){
    var ongoingRequest = false; // This is to deal with strange Firefox behavior
                                //  where .getCurrentPosition only fires callback once
                                //  even when called multiple times

    return function(){
        var deferred = $q.defer();

        if(!ongoingRequest){
            // store promise
            ongoingRequest = deferred.promise;
            navigator.geolocation.getCurrentPosition(function(pos){
                deferred.resolve({latitude: pos.coords.latitude, longitude: pos.coords.longitude});
                ongoingRequest = undefined;
            }, function(){
                deferred.reject();
                ongoingRequest = undefined;
            });
        }else{
            // reuse previous promise
            ongoingRequest.then(function(latlng){
                deferred.resolve(latlng);
            }, function(){
                deferred.reject();
            });
        }

        return deferred.promise;
    };
})
Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247