• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Obtaining Device Location Information (C/C++)
2
3
4## When to Use
5
6You can call location-related APIs to listen for device location changes.
7
8## Function Description
9
10| Name                                                        | Description                                                        |
11| ------------------------------------------------------------ | ------------------------------------------------------------ |
12| OH_Location_IsLocatingEnabled(bool* enabled)     | Checks whether the location switch is enabled.                                |
13| OH_Location_StartLocating(const Location_RequestConfig* requestConfig) | Starts locating and subscribes to location changes.|
14| Location_ResultCode OH_Location_StopLocating(const Location_RequestConfig* requestConfig) | Stops locating and unsubscribes from location changes.|
15| OH_LocationInfo_GetBasicInfo(Location_Info* location)   | Obtains basic information from the location result, such as the longitude, latitude, altitude, and speed.|
16| OH_LocationInfo_GetAdditionalInfo(Location_Info* location, char* additionalInfo, uint32_t length) | Obtains additional information from the location result. The additional information is a string in JSON format.|
17| OH_Location_CreateRequestConfig(void) | Creates a **Location_RequestConfig** instance.   |
18| OH_Location_DestroyRequestConfig(Location_RequestConfig* requestConfig) | Destroys the **Location_RequestConfig** instance and reclaims the memory. |
19| OH_LocationRequestConfig_SetUseScene(Location_RequestConfig* requestConfig, Location_UseScene useScene) | Sets the user activity scenario in a location request.<br>If **useScene** is set, **powerConsumptionScene** is invalid.<br>Otherwise, **powerConsumptionScene** takes effect.<br>If neither of the two parameters is set, **useScene** is defaulted to **LOCATION_USE_SCENE_DAILY_LIFE_SERVICE**, and **powerConsumptionCenario** is invalid.    |
20| OH_LocationRequestConfig_SetPowerConsumptionScene(Location_RequestConfig* requestConfig, Location_PowerConsumptionScene powerConsumptionScene) | Sets the power consumption scenario in a location request.     |
21| OH_LocationRequestConfig_SetInterval(Location_RequestConfig* requestConfig, int interval) | Sets the interval for reporting location results.                              |
22| OH_LocationRequestConfig_SetCallback(Location_RequestConfig* requestConfig, Location_InfoCallback callback, void* userData) | Sets the callback for receiving reported location information.    |
23
24
25## How to Develop
261. Create a native C++ project.
27   ![](figures/001.png)
28
292. Before using system basic location capabilities, check whether your application has been granted the permission to access the device location information. If not, your application first needs to apply for the required permission. For details, see [Applying for Location Permissions](location-permission-guidelines.md).
30
31
323. Add the dynamic dependency libraries into the **CMakeLists.txt** file.
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. Write the **napi_init.cpp** file to import related modules.
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. Call **isLocationEnabled** to check whether the location switch is enabled.
51   The return result is a Boolean value. The value **true** indicates that the location switch is enabled, and the value **false** indicates the opposite.
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    // Add related APIs to the Init function.
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. Start positioning and subscribe to location changes.
76
77    ```c
78    // Define a location request.
79    struct Location_RequestConfig *g_requestConfig = NULL;
80    void *mydata = NULL;
81
82    // Define a callback to receive location information.
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    // Subscribe to location information.
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")); // Custom data, which is transparently returned through 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    // Unsubscribe from location information. The value of g_requestConfig must be the same as the object passed during subscription.
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    // Add related APIs to the Init function.
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. Introduce the NAPI APIs to the **index.d.ts** file in **types/libentry**.
142    ```c
143     export const ohLocationIsEnabled: () => boolean;
144     export const ohLocationStartLocating: () => number;
145     export const ohLocationStopLocating: () => number;
146    ```
147
1487. Delete deprecated functions from the **Index.ets** file.
149
150    ```js
151    .onClick(() => {
152        hilog.info(0x0000, 'testTag', 'Test NAPI 2 + 3 = %{public}d', testNapi.add(2, 3));
153    })
154    ```
155