安装:
$ ionic cordova plugin add cordova-plugin-geolocation --variable GEOLOCATION_USAGE_DESCRIPTION="To locate you"
$ 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) { }
代码:
this.geolocation.getCurrentPosition().then((resp) => {
console.log(resp.coords.latitude ); //纬度
console.log(resp.coords.longitude); //经度
}).catch((error) => { console.log('Error getting location', error);});