Ionic 3 geolocation not working when GPS is disabl

2019-02-27 22:36发布

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条回答
兄弟一词,经得起流年.
2楼-- · 2019-02-27 23:00

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.

查看更多
登录 后发表回答