摘要
ionic4 獲取地理位置信息的細(xì)節(jié)有如下關(guān)鍵點(diǎn)
1、導(dǎo)入路徑為@ionic-native/geolocation/ngx斯棒,而非@ionic-native/geolocation
2瘸右、在*.module.ts的providers中加入Geolocation
詳情
安裝
ionic cordova plugin add cordova-plugin-geolocation
npm install @ionic-native/geolocation
安裝完成后,在package.json中會(huì)自動(dòng)添加配置信息:
1)dependencies中會(huì)自動(dòng)添加"@ionic-native/geolocation":"*.*.*"
2) 自動(dòng)添加cordova? plugins信息:cordova-plugin-geolocation
使用
1、在需要使用的模塊中添加Geolocation模塊仅仆,例如app.module.ts
import {Geolocation} from "@ionic-native/geolocation/ngx"
……
providers: [
Geolocation,
……],……
2、需要使用的組件中添加引用關(guān)系垢袱。
import {Geolocation} from "@ionic-native/geolocation/ngx"
注意:默認(rèn)不會(huì)添加Geolocation引用關(guān)系墓拜,因?yàn)楹湍J(rèn)庫(kù)中的Geolocation重名。
3请契、調(diào)用getCurrentPosition可以獲取位置信息咳榜。
this.geolocation.getCurrentPosition().then((resp) => {
???? console.log(resp.coords.latitude);
???? console.log(resp.coords.longitude);
}).catch((error) => {
???? console.log('Error getting location', error);
});
注意:早期版本的getCurrentPosition用法不同,需要填入onSuccess爽锥,onError等參數(shù)涌韩。
????????? 新版采用.then、.catch的方式氯夷。
參考
3肠槽、Ionic3+GPS定位