安装:
$ 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);});
博客介绍了在Ionic3项目中使用地理定位插件的方法。首先给出了安装命令,包括添加Cordova插件和安装相关npm包。接着说明了在app.module.ts中导入并在providers声明,以及在项目ts文件中导入和使用的代码示例,还展示了获取经纬度的代码。
1094

被折叠的 条评论
为什么被折叠?



