我不能让离子cordova Geolocation插件在Android或浏览器上运行,尽管相同的代码适用于iOS . 即使在我获得许可后,getCurrentPosition方法也无法访问该位置 . 在android上,getCurrentPosition永远不会返回,除非我添加一个超时参数,然后它返回错误“Timeout expired . ”我在Android模拟器和实际设备上运行它(运行OS 6和7),Chrome和Safari,没有哪个工作 .

这是我的离子/系统信息:

  • @ ionic / cli-utils:1.12.0

  • ionic(离子CLI):3.12.0

  • cordova(Cordova CLI):7.0.1

  • @ ionic / app-scripts:2.1.4

  • Cordova平台:android 6.2.3浏览器4.1.0 ios 4.4.0

  • 离子框架:离子角度3.6.1

  • cordova-plugin-geolocation:2.4.3

  • 操作系统:OS X El Capitan

我试过这些(以及更多变化):

constructor(public navCtrl: NavController,
              private geolocation: Geolocation) {
  navigator.geolocation.getCurrentPosition(this.onSuccess, this.onError, {timeout:5000});
  
}

onSuccess(position) {
  alert(JSON.stringify(position));
}

onError(error) {
  alert('code: '+ error.code+ '\n'+'message: '+error.message + '\n');
}
this.geolocation.getCurrentPosition({timeout:5000}).then((pos : Geoposition) => {
  alert(pos+" : " +JSON.stringify(pos));
}.catch((err : PositionError)=>{
  alert("error : " + err.message);
});

如上所述,两者都给出相同/相似的结果