• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 获取设备的位置信息开发指导(C/C++)
2
3
4## 场景介绍
5
6开发者可以调用OpenHarmony位置相关接口,监听设备的位置变化。
7
8## 函数说明
9
10| 名称                                                         | 描述                                                         |
11| ------------------------------------------------------------ | ------------------------------------------------------------ |
12| OH_Location_IsLocatingEnabled(bool* enabled)     | 查询位置开关是否开启。                                 |
13| OH_Location_StartLocating(const Location_RequestConfig* requestConfig) | 启动定位并订阅位置变化。 |
14| Location_ResultCode OH_Location_StopLocating(const Location_RequestConfig* requestConfig) | 停止定位并取消订阅位置变化。 |
15| OH_LocationInfo_GetBasicInfo(Location_Info* location)   | 从定位结果中获取基本信息,如经纬度、海拔、速度等信息。 |
16| OH_LocationInfo_GetAdditionalInfo(Location_Info* location, char* additionalInfo, uint32_t length) | 从定位结果中获取附加信息。附加信息是一个JSON格式的字符串。 |
17| OH_Location_CreateRequestConfig(void) | 创建一个位置请求参数结构体实例。    |
18| OH_Location_DestroyRequestConfig(Location_RequestConfig* requestConfig) | 销毁位置请求参数实例并回收内存。  |
19| OH_LocationRequestConfig_SetUseScene(Location_RequestConfig* requestConfig, Location_UseScene useScene) | 设置发起定位时的用户活动场景。<br/>如果设置了useScene,则powerConsumptionScene无效。<br/>如果未设置useScene,且设置了powerConsumptionScene,则该参数生效。<br/>如果两个参数都不设置,则默认useScene为LOCATION_USE_SCENE_DAILY_LIFE_SERVICE,powerConsumptionCenario参数无效。     |
20| OH_LocationRequestConfig_SetPowerConsumptionScene(Location_RequestConfig* requestConfig, Location_PowerConsumptionScene powerConsumptionScene) | 设置发起定位时的功耗场景。      |
21| OH_LocationRequestConfig_SetInterval(Location_RequestConfig* requestConfig, int interval) | 设置定位结果上报时间间隔。                               |
22| OH_LocationRequestConfig_SetCallback(Location_RequestConfig* requestConfig, Location_InfoCallback callback, void* userData) | 设置用于接收位置上报的回调函数。     |
23
24
25## 开发步骤
261. 新建一个Native C++工程。
27   ![输入图片说明](figures/001.png)
28
292. 获取设备的位置信息,需要有位置权限,位置权限申请的方法和步骤见[申请位置权限开发指导](location-permission-guidelines.md)。
30
31
323. CMakeLists.txt文件中引入动态依赖库。
33
34   ```c
35   target_link_libraries(entry PUBLIC libace_napi.z.so)
36   target_link_libraries(entry PUBLIC libhilog_ndk.z.so)
37   target_link_libraries(entry PUBLIC liblocation_ndk.so)
38   ```
39
404. 在napi_init.cpp文件中编码,首先导入模块。
41
42   ```c
43   #include "napi/native_api.h"
44   #include "LocationKit/oh_location.h"
45   #include "LocationKit/oh_location_type.h"
46   #include "hilog/log.h"
47   #include <stdlib.h>
48   ```
49
505. 调用获取位置接口之前需要先判断位置开关是否打开。
51   查询当前位置开关状态,返回结果为布尔值,true代表位置开关开启,false代表位置开关关闭,示例代码如下:
52
53   ```c
54    static napi_value OhLocationIsEnabled(napi_env env, napi_callback_info info)
55    {
56        bool isEnabled = false;
57        int resultCode = OH_Location_IsLocatingEnabled(&isEnabled);
58        napi_value result = NULL;
59        napi_get_boolean(env, isEnabled, &result);
60        return result;
61    }
62    // 在Init函数中补充接口。
63    EXTERN_C_START
64    static napi_value Init(napi_env env, napi_value exports)
65    {
66        napi_property_descriptor desc[] = {
67            {"ohLocationIsEnabled", NULL, OhLocationIsEnabled, NULL, NULL, NULL, napi_default, NULL},
68        };
69        napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
70        return exports;
71    }
72    EXTERN_C_END
73   ```
74
756. 定位位置变化。
76
77    ```c
78    // 定义一个请求参数
79    struct Location_RequestConfig *g_requestConfig = NULL;
80    void *mydata = NULL;
81
82    // 定义一个回调函数用来接收位置信息
83    void reportLocation(Location_Info* location, void* userData)
84    {
85        Location_BasicInfo baseInfo = OH_LocationInfo_GetBasicInfo(location);
86        char additionalInfo[1024] = "";
87        Location_ResultCode result = OH_LocationInfo_GetAdditionalInfo(location, additionalInfo, sizeof(additionalInfo));
88        if (mydata == userData) {
89            OH_LOG_INFO(LOG_APP, "userData is mydata");
90        }
91        return;
92    }
93
94    // 订阅位置信息
95    static napi_value OhLocationStartLocating(napi_env env, napi_callback_info info)
96    {
97        if (g_requestConfig == NULL) {
98            g_requestConfig = OH_Location_CreateRequestConfig();
99        }
100        OH_LocationRequestConfig_SetUseScene(g_requestConfig, LOCATION_USE_SCENE_NAVIGATION);
101        OH_LocationRequestConfig_SetInterval(g_requestConfig, 1);
102        mydata = (void *)malloc(sizeof("mydata")); // 用户自定义任意类型,callback 透传返回
103        OH_LocationRequestConfig_SetCallback(g_requestConfig, reportLocation, mydata);
104        OH_Location_StartLocating(g_requestConfig);
105        int32_t ret = 0;
106        napi_value result = NULL;
107        napi_create_int32(env, ret, &result);
108        return result;
109    }
110
111    //取消订阅位置信息, g_requestConfig要和订阅时传入的对象保持一致
112    static napi_value OhLocationStopLocating(napi_env env, napi_callback_info info)
113    {
114        OH_Location_StopLocating(g_requestConfig);
115        if (g_requestConfig != NULL) {
116            OH_Location_DestroyRequestConfig(g_requestConfig);
117            g_requestConfig = NULL;
118        }
119        free(mydata);
120        mydata = NULL;
121        int32_t ret = 0;
122        napi_value result = NULL;
123        napi_create_int32(env, ret, &result);
124        return result;
125    }
126
127    // 在Init函数中补充接口。
128    EXTERN_C_START
129    static napi_value Init(napi_env env, napi_value exports)
130    {
131        napi_property_descriptor desc[] = {
132            {"ohLocationStartLocating", NULL, OhLocationStartLocating, NULL, NULL, NULL, napi_default, NULL},
133            {"ohLocationStopLocating", NULL, OhLocationStopLocating, NULL, NULL, NULL, napi_default, NULL},
134        };
135        napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
136        return exports;
137    }
138    EXTERN_C_END
139    ```
140
1416. 在types/libentry路径下index.d.ts文件中引入Napi接口。
142    ```c
143     export const ohLocationIsEnabled: () => boolean;
144     export const ohLocationStartLocating: () => number;
145     export const ohLocationStopLocating: () => number;
146    ```
147
1487. 删除Index.ets中的已废弃函数。
149
150    ```js
151    .onClick(() => {
152        hilog.info(0x0000, 'testTag', 'Test NAPI 2 + 3 = %{public}d', testNapi.add(2, 3));
153    })
154    ```