地理位置警报不会停留在屏幕上

Geolocation alert won't stay on screen

我正在尝试在我的网络应用程序上实施基本地理定位,但 Chrome 中的警报不会保留在屏幕上;它在底部弹出,但在我单击任何选项之前就消失了。

geolocation alert won't stay on screen

您缺少一个错误处理函数。尝试添加:

  var success = function(position) {
    //do something with position
  };
  var failure = function(error) {
    console.log('Error occurred. Error code: ' + error.code);
    // error.code can be:
    //   0: unknown error
    //   1: permission denied
    //   2: position unavailable (error response from location provider)
    //   3: timed out
  };
  navigator.geolocation.getCurrentPosition(success, failure);

此外,考虑添加 {timout: 5000}(或任何其他时间长度)作为第三个参数,因为函数的 default timeout to search for location is Infinity. See the documentation on MDN.