【HarmonyOS NEXT】鸿蒙应用地理位置获取,地理名称获取 原创
头像 GeorgeGcs 2025-06-30 23:25:29    发布
2196 浏览 3 点赞 0 收藏

## 【HarmonyOS NEXT】鸿蒙应用地理位置获取,地理名称获取


##鸿蒙开发能力 ##HarmonyOS SDK应用服务##鸿蒙金融类应用 (金融理财#


## 一、前言


首先要理解地理专有名词,当我们从系统获取地理位置,一般会拿到地理坐标,是一串数字,并不是地理位置名称。例如 116.2305,33.568。


这些数字坐标会有不同的**坐标系**,国际上一般使用 **wgs84** (WGS 84是全球定位系统(GPS)的基准坐标系统,广泛应用于全球定位和导航。它采用十进制度表示经度和纬度。)


但是国内一般会使用加密坐标系,**GCJ-02** (中国采用的加密坐标系,也称为火星坐标系,对WGS 84坐标进行加密偏移。)


拿到坐标参数x,y后,我们需要通过**逆地理编码**,将坐标转化为地理描述。地里描述,包括国家、行政区划、街道、门牌号、地址描述等。


## 二、地址位置获取和逆地址编码转化:

首先我们需要获取当前设备的地理位置,该行为需要权限配置,用户同意后,才能拿到当前定位的地址位置。

![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/e490ad0e55e84b4db55bfdb2074d5cd1.png)

**1. 配置定位权限**


```dart

     {

       "name": "ohos.permission.APPROXIMATELY_LOCATION",

       "reason": "$string:reason",

       "usedScene": {

         "abilities": [

           "EntryAbility"

         ],

         "when": "always"

       }

     },

     {

       "name": "ohos.permission.LOCATION",

       "reason": "$string:reason",

       "usedScene": {

         "abilities": [

           "EntryAbility"

         ],

         "when": "always"

       }

     },

```


**2.申请用户动态权限**


```dart

   abilityAccessCtrl.createAtManager().requestPermissionsFromUser(getContext(), [

     'ohos.permission.LOCATION', 'ohos.permission.APPROXIMATELY_LOCATION']).then(() => {

       // 权限申请通过后, 获取当前位置


   });

```


**3.导入位置服务。获取地理位置信息,进行逆地理编码获取当前位置。**


```dart

import { geoLocationManager } from '@kit.LocationKit';


       private locationChange: (err: BusinessError, location: geoLocationManager.Location) => void = (err, location) => {


   if (location) {

     let reverseGeocodeRequest: geoLocationManager.ReverseGeoCodeRequest = {

       'latitude': location.latitude,

       'longitude': location.longitude,

       'maxItems': 1

     };


     // 逆地址编码转化,获取地址位置描述

     geoLocationManager.getAddressesFromLocation(reverseGeocodeRequest, (err, data) => {

       if (data) {

         hilog.info(0x00000, 'getAddressesFromLocation: data=', JSON.stringify(data));

         if (data[0].locality !== undefined) {

           let local = data[0].locality.replace(/"/g, '').slice(0, -1);

           let currentLocal = data[0].locality.replace(/"/g, '').slice(0, -1);

           console.log(this.TAG, " local: " + local + " currentLocal: " + currentLocal)

         }

       }

     });

   }

 };


    geoLocationManager.getCurrentLocation(this.locationChange);

```


## 三、DEMO完整示例:

![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/2f419a7090ef450999da345fb8e33832.png)



```dart

import { abilityAccessCtrl } from '@kit.AbilityKit';

import { geoLocationManager } from '@kit.LocationKit';

import { BusinessError } from '@kit.BasicServicesKit';

import { hilog } from '@kit.PerformanceAnalysisKit';


@Entry

@Component

struct LocationPage {


 private TAG: string = "LocationPage";


 /**

  * 定位回调

  */

 private locationChange: (err: BusinessError, location: geoLocationManager.Location) => void = (err, location) => {

   if (err) {

     //

     console.log(this.TAG, " locationChanger: err=: " + JSON.stringify(err))

   }

   if (location) {

     let reverseGeocodeRequest: geoLocationManager.ReverseGeoCodeRequest = {

       'latitude': location.latitude, // 表示纬度信息,正值表示北纬,负值表示南纬。取值范围为-90到90。仅支持WGS84坐标系。

       'longitude': location.longitude, // 表示经度信息,正值表示东经,负值表是西经。取值范围为-180到180。仅支持WGS84坐标系。

       // 指定返回位置信息的最大个数。取值范围为大于等于0,推荐该值小于10。默认值是1。

       'maxItems': 1

     };


     // 逆地址编码转化,获取地址位置描述

     geoLocationManager.getAddressesFromLocation(reverseGeocodeRequest, (err, data) => {

       if (data) {

         hilog.info(0x00000, 'getAddressesFromLocation: data=', JSON.stringify(data));

         if (data[0].locality !== undefined) {

           let local = data[0].locality.replace(/"/g, '').slice(0, -1);

           let currentLocal = data[0].locality.replace(/"/g, '').slice(0, -1);

           console.log(this.TAG, " local: " + local + " currentLocal: " + currentLocal)

         }

       }

     });

   }

 };


 onClickGetLocation = ()=>{

   // 请求用户同意权限

   abilityAccessCtrl.createAtManager().requestPermissionsFromUser(getContext(), [

     'ohos.permission.LOCATION', 'ohos.permission.APPROXIMATELY_LOCATION']).then(() => {

       // 获取当前位置

     geoLocationManager.getCurrentLocation(this.locationChange);


   });

 }



 build() {

   RelativeContainer() {

     Text("获取当前定位信息")

       .id('LocationPageHelloWorld')

       .fontSize(50)

       .fontWeight(FontWeight.Bold)

       .alignRules({

         center: { anchor: '__container__', align: VerticalAlign.Center },

         middle: { anchor: '__container__', align: HorizontalAlign.Center }

       })

       .onClick(this.onClickGetLocation)

   }

   .height('100%')

   .width('100%')

 }

}

```

![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/6c6080222c6a4e63ae6cfe63114f1cfb.png)

一般获取定位,还需要配置网络权限,用于方便系统定位。


```dart

     {

       "name": "ohos.permission.INTERNET",

       "reason": "$string:reason",

       "usedScene": {

         "abilities": [

           "EntryAbility"

         ],

         "when": "always"

       }

     }

```

![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/7c28bfcd44004256a64fa5601ca66766.png)


©本站发布的所有内容,包括但不限于文字、图片、音频、视频、图表、标志、标识、广告、商标、商号、域名、软件、程序等,除特别标明外,均来源于网络或用户投稿,版权归原作者或原出处所有。我们致力于保护原作者版权,若涉及版权问题,请及时联系我们进行处理。
分类
HarmonyOS

暂无评论数据

发布

头像

GeorgeGcs

HarmonyOS认证学习资源创作专家,华为HDE专家,鸿蒙讲师,作者。目前任职鸿蒙应用架构师。 历经腾讯,宝马,研究所,金融。 待过私企,外企,央企。 深耕大应用开发领域十年。 OpenHarmony,HarmonyOS,Flutter,H5,Android,IOS。

125

帖子

1

提问

506

粉丝

关注
热门推荐
地址:北京市朝阳区北三环东路三元桥曙光西里甲1号第三置业A座1508室 商务内容合作QQ:2291221 电话:13391790444或(010)62178877
版权所有:电脑商情信息服务集团 北京赢邦策略咨询有限责任公司
声明:本媒体部分图片、文章来源于网络,版权归原作者所有,我司致力于保护作者版权,如有侵权,请与我司联系删除
京ICP备:2022009079号-2
京公网安备:11010502051901号
ICP证:京B2-20230255