Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
166 views
in Technique[技术] by (71.8m points)

javascript - Browser Geolocation API not throwing error or respecting timeout

This has felt like black magic to me for some time, and I'm hoping one of you can help clear this up. I have a geolocation function in JS that goes as follows:

// User Position
export function getPosition(options) {
  return new Promise(function (resolve, reject) {
    function success(pos) {
      var crd = pos.coords;
      console.log('we have succeeded with ' + crd)
      resolve(pos);
    }

    function failure(err) {
      console.warn(`ERROR(${err.code}): ${err.message}`);
      reject();
    }

    var options = {
      enableHighAccuracy: true,
      timeout: 5500,
      maximumAge: 6000
    };

    // debugger;
    navigator.geolocation.getCurrentPosition(success, failure, options);
  });
};

and it is called within an async function with:

let position = await getPosition();

I have identified two cases where the promise is never resolved, somewhat predictably even with a timeout set.

  • Browser is updated in iOS or MacOS and the new privacy settings revoke location access.
  • Opened from a link in safari or chrome on iOS device

The getPosition() function will hang indefinitely, or far exceeding the timeout duration before the user is prompted.

Am I missing something in my code, or is this specific to new privacy settings with Apple operating systems where the geolocation API is falling on it's face?

question from:https://stackoverflow.com/questions/65946745/browser-geolocation-api-not-throwing-error-or-respecting-timeout

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you look here you'll see my matching observations: -

    const TIMEOUT_IS_USELESS
                       = Number.POSITIVE_INFINITY;

and

            trackerId = navigator.geolocation.watchPosition(filterLocation, locError, {
                    enableHighAccuracy: accurate,
                    maximumAge: maxLocAge,
                    timeout: TIMEOUT_IS_USELESS
                });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...