親測iphone6,紅米note2,紅米note4可以獲取經緯度數據,魅藍note3可以(要打開定位服務),廢話少說,按官網步驟,
- 下載插件:
ionic plugin add --save cordova-plugin-geolocation npm install --save @ionic-native/geolocation
- 在app.module.ts中導入,注意,providers中要聲明:
import { Geolocation } from '@ionic-native/geolocation';
providers: [ StatusBar, SplashScreen, Geolocation, {provide: ErrorHandler, useClass: IonicErrorHandler} ]
- 在你要用的ts文件中使用此服務,看官網:
import { Geolocation } from '@ionic-native/geolocation'; constructor(private geolocation: Geolocation) { }
``
那么問題來了,@ionic-native/geolocation/index.d.ts中IonicNativePlugin報錯,說@ionic-native/core里頭沒有這玩意。
解決辦法:查看package.json,@ionic-native/core的版本,我這里是"@ionic-native/core": "^3.6.1",目前最新版,如果你的不是最新版,則更新到最新,執行
npm uninstall --save @ionic-native/core npm install --save @ionic-native/core@latest
搞定!