# location **Repository Path**: harmony-free/location ## Basic Information - **Project Name**: location - **Description**: Harmony OS location 鸿蒙位置定位库 - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2025-05-23 - **Last Updated**: 2025-07-22 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # location #### 介绍 [English](README.en.md) Harmony OS location 鸿蒙定位库。获取当前位置、当前GCJ02位置信息、获取定位、获取附近定位、根据经纬度 获取附近定位、根据地址 获取附近定位、根据地址或经纬度 获取附近定位、转化GCJ02定位 #### 软件架构 基于系统定位请求库 Location 封装。 #### 安装教程 `ohpm install @free/location` #### 使用说明 1、初始化 ``` local:Location = new Location() /** * 当前位置信息 * @returns geoLocationManager.Location */ this.local.currentLocation() /** * 当前GCJ02位置信息 * @returns geoLocationManager.Location */ this.local.currentLocationGCJ02() /** * 获取定位 * @returns geoLocationManager.GeoAddress */ this.local.location() /** * 获取附近定位 * @returns Array */ this.local.locationList() /** * 根据经纬度 获取附近定位 * @returns Array */ this.local.locationAddress() /** * 根据地址 获取附近定位 * description:string * @returns Array */ this.local.locationAddressName() /** * 根据地址或经纬度 获取附近定位 * @returns Array */ this.local.locationAddressList() /** * 转化GCJ02定位 * @returns mapCommon.LatLng */ toGCJ02(latLong:mapCommon.LatLng) ``` #### 插件明细 hello 各位同学,大家好! 今天我们来讲讲关于鸿蒙里常用的定位功能,在开发的地图、附近的人、收货地址、附近商场等功能的过程中基本都要使用定位功能,定位跟生活息息相关,不同的国家采用的定位计算方式都不一样,不同的app的定位也不一样。 1、根据地址名称或经纬度获取定位,或者获取附近位置 ``` /** * 获取定位 * @returns geoLocationManager.GeoAddress */ async location(data?:string|mapCommon.LatLng|undefined): Promise { return new Promise((res, rej) => { try { if (data == undefined) { this.currentLocation().then((location)=>{ this.locationAddress({latitude:location.latitude,longitude:location.longitude}).then((list)=>{ if (list.length > 0) { res(list[0]); }else{ console.error("errCode:" + "500" + ", message:" + "数据为空"); rej("errCode:" + "500" + ", message:" + "数据为空"); } }) }) }else { this.locationAddressList(data).then((list)=>{ if (list.length > 0) { res(list[0]); }else{ console.error("errCode:" + "500" + ", message:" + "数据为空"); rej("errCode:" + "500" + ", message:" + "数据为空"); } }) } } catch (err) { console.error("errCode:" + err.code + ", message:" + err.message); rej(err) } }) } ``` 2、定位装换,WGS84转换GCJ02 定位 和 GCJ02转换WGS84 ``` toGCJ02(latLong:mapCommon.LatLng):mapCommon.LatLng{ return map.convertCoordinateSync(mapCommon.CoordinateType.WGS84, mapCommon.CoordinateType.GCJ02, latLong); } toWGS84(latLong:mapCommon.LatLng):mapCommon.LatLng{ return map.convertCoordinateSync(mapCommon.CoordinateType.GCJ02, mapCommon.CoordinateType.WGS84, latLong); } ``` 注意:完整代码我已提交到[鸿蒙三方库](https://ohpm.openharmony.cn/#/cn/home)中,使用一下命令安装 喜欢本篇内容的话给个小爱心! #### 参与贡献 1. Fork 本仓库 2. 新建 Feat_xxx 分支 3. 提交代码 4. 新建 Pull Request #### 特技 1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md 2. Gitee 官方博客 [blog.gitee.com](https://blog.gitee.com) 3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解 Gitee 上的优秀开源项目 4. [GVP](https://gitee.com/gvp) 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目 5. Gitee 官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help) 6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)