获取地理位置:navigator.geolocation.getCurrentPosition 无效

MDN的例子,本地运行没有打印地理位置,也没有报错,为何?求指导
刚开始PC端一直返回超时,可能是PC端不支持,用手机打开后又提示:error.code 1 PERMISSION_DENIED

手机谷歌浏览器报错:err.code 1,不允许获取地理位置,如何解决?
哪里可以重新打开允许或者js如何修改是否允许?

    var options = {
      enableHighAccuracy: true,
      timeout: 5000,
      maximumAge: 0
    };
    ​
    function success(pos) {
      var crd = pos.coords;
    ​
      console.log('Your current position is:');
      console.log('Latitude : ' + crd.latitude);
      console.log('Longitude: ' + crd.longitude);
      console.log('More or less ' + crd.accuracy + ' meters.');
    };
    ​
    function error(err) {
      console.warn('ERROR(' + err.code + '): ' + err.message);
};
​
navigator.geolocation.getCurrentPosition(success, error, options);

clipboard.png

阅读 20.2k
3 个回答

navigator.geolocation.getCurrentPosition在谷歌浏览器好像不行,其他浏览器可以,具体原因不清楚

getCurrentPosition是异步的,调用后并不会立即返回结果。

你的截图里面已经打印错误信息了,就是ERROR(3): Timeout expired,说明请求超时了。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题