問題描述
我安裝了離子原生地理定位插件
I have ionic native geolocation plugin installed
"@ionic-native/geolocation": "^4.15.0"
我也試過4.6.0"和4.20.0".當我在進入該頁面之前啟用 GPS 時,它工作得非常好.但是當 GPS 未啟用時,它不會要求我打開它,在控制臺上給出錯誤并攜帶未定義的坐標.
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.
我在constructor/ionViewDidLoad中寫了getCurrentPosition的方法.因此,即使用戶在該頁面上啟用它,該方法也不會調用并且坐標仍然未定義.
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.
以下是代碼
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.
推薦答案
您應該手動請求權限并請求用戶啟用位置.您可以使用診斷插件 (@ionic-native/diagnostic) 執行此操作.您應該使用以下方法:
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.
這篇關于禁用 GPS 時,Ionic 3 地理定位不起作用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!