- 安装 geolocation 插件 执行以下命令
npm install --save @ionic-native/geolocation
- 将geolocation插件在app.module.ts内声明
import { Geolocation } from '@ionic-native/geolocation'; @NgModule({ providers: [ { provide: ErrorHandler, useClass: IonicErrorHandler }, Geolocation ] })
- 在使用geolocation插件的页面导入该插件并且依赖注入
import { Injectable } from '@angular/core'; import { Geolocation } from '@ionic-native/geolocation'; @Injectable() export class GeolocationService { constructor( private geolocation: Geolocation ) {} }
- 使用高德地图与百度地图需在index.html页引入相关js。高德地图的key值可更改为您所申请的key值
<script type="text/javascript" src="http://webapi.amap.com/maps?v=1.4.1&key=99f20bf360cfecd1eb94acfcb6819474"></script> <script type="text/javascript" src="http://developer.baidu.com/map/jsdemo/demo/convertor.js"></script>
- 使用geolocation插件的方法获取当前位置经纬度信息
/** * 获取当前地理位置 */ getCurrentPosition(): Promise<{ latitude: string, longitude: string }> { return this.geolocation.getCurrentPosition().then(res => { let latitude = res.coords.latitude.toString(); //纬度
ionic2 geolocation定位,将geolocation坐标转换为百度坐标,高德地图坐标
最新推荐文章于 2024-03-02 10:25:13 发布