Ionic 3 geolocation not working when GPS is disabled

半城伤御伤魂 提交于 2019-12-20 02:12:31

问题


I have ionic native geolocation plugin installed

"@ionic-native/geolocation": "^4.15.0"

I have also tried "4.6.0" and "4.20.0". It is working absolutely fine when I keep my GPS enabled before going to that page. But when GPS is not enabled, It won't ask me to turn it ON, gives an error on console and carry undefined coordinates with it.

I wrote the method of getCurrentPosition in constructor/ionViewDidLoad. So even user enable it on that page, the method does not invoke and the coordinates remain undefined.

Following is code

this.geolocation
  .getCurrentPosition()
  .then(resp => {
    console.log(resp);
    this.longitude = resp.coords.longitude;
    this.latitude = resp.coords.latitude;
  })
  .catch(error => {
    console.log("Error getting location", error);
  });

I don't know if I'll have to give manual permissions or what?? I did the same before a couple of months before and everything was fine. First time I am facing this kind of issue. Please help me to get out of this.


回答1:


you should manually ask permission and request the user to enable location. You can do this with the Diagnostic plugin (@ionic-native/diagnostic). You should use the following methods:

diagnostic.isLocationEnabled()
diagnostic.isLocationAvailable()
diagnostic.requestLocationAuthorization()

If you want to update location after permission is granted you you can use this method:

diagnostic.registerLocationStateChangeHandler()

You can pass a callback here check if location is enabled and available de what you need.




回答2:


Install the Diagnostics plugin from here: https://ionicframework.com/docs/v3/native/diagnostic/, then check if location is enabled with diagnostics.isLocationAvailable(), if not, prompt the user to enable it from the device settings screen using diagnostics.switchToLocationSettings().



来源:https://stackoverflow.com/questions/54592356/ionic-3-geolocation-not-working-when-gps-is-disabled

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!