1e41f4b71Sopenharmony_ci# Obtaining Device Location Information 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci## When to Use 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ciYou can call location APIs in OpenHarmony to obtain the real-time location or last known location and keep track of location changes of a device. 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ciReal-time location of the device is recommended for location-sensitive services. If you want to lower power consumption when the real-time location of the device is not needed, you may consider obtaining the last known location of the device. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci## Available APIs 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ciThe following table lists the APIs used to obtain the device location information. For details, see (../../reference/apis-location-kit/js-apis-geoLocationManager.md). 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci**Table 2** APIs for obtaining device location information 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci| API| Description| 16e41f4b71Sopenharmony_ci| -------- | -------- | 17e41f4b71Sopenharmony_ci| [on(type: 'locationChange', request: LocationRequest | ContinuousLocationRequest, callback: Callback<Location>): void](../../reference/apis-location-kit/js-apis-geoLocationManager.md#geolocationmanageronlocationchange) | Registers a listener for location changes with a location request initiated.| 18e41f4b71Sopenharmony_ci| [off(type: 'locationChange', callback?: Callback<Location>): void](../../reference/apis-location-kit/js-apis-geoLocationManager.md#geolocationmanagerofflocationchange) | Unregisters the listener for location changes with the corresponding location request deleted.| 19e41f4b71Sopenharmony_ci| [getCurrentLocation(request: CurrentLocationRequest | SingleLocationRequest, callback: AsyncCallback<Location>): void](../../reference/apis-location-kit/js-apis-geoLocationManager.md#geolocationmanagergetcurrentlocation) | Obtains the current location. This API uses an asynchronous callback to return the result. | 20e41f4b71Sopenharmony_ci| [getCurrentLocation(request?: CurrentLocationRequest | SingleLocationRequest): Promise<Location>](../../reference/apis-location-kit/js-apis-geoLocationManager.md#geolocationmanagergetcurrentlocation-2) | Obtains the current location. This API uses a promise to return the result. | 21e41f4b71Sopenharmony_ci| [getLastLocation(): Location](../../reference/apis-location-kit/js-apis-geoLocationManager.md#geolocationmanagergetlastlocation) | Obtains the last known device location.| 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci## How to Develop 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci1. 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). 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci2. Import the **geoLocationManager** module by which you can implement all APIs related to the basic location capabilities. 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci ```ts 30e41f4b71Sopenharmony_ci import { geoLocationManager } from '@kit.LocationKit'; 31e41f4b71Sopenharmony_ci ``` 32e41f4b71Sopenharmony_ci3. Obtain the current location of the device. It is mainly used in scenarios such as viewing of the current location, signing in/out, and service recommendation. 33e41f4b71Sopenharmony_ci - Method 1: Obtain the latest location in the system cache.<br> 34e41f4b71Sopenharmony_ci If the system does not have a cached location, an error code is returned.<br> 35e41f4b71Sopenharmony_ci Using this API to obtain the device location can reduce the system power consumption.<br> 36e41f4b71Sopenharmony_ci If a fresh location is expected, your application can first obtain the cached location and then compare the timestamp in it with the current time. If the location is not fresh, try method 2.<br> 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci ```ts 39e41f4b71Sopenharmony_ci import { geoLocationManager } from '@kit.LocationKit'; 40e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit' 41e41f4b71Sopenharmony_ci try { 42e41f4b71Sopenharmony_ci let location = geoLocationManager.getLastLocation(); 43e41f4b71Sopenharmony_ci } catch (err) { 44e41f4b71Sopenharmony_ci console.error("errCode:" + JSON.stringify(err)); 45e41f4b71Sopenharmony_ci } 46e41f4b71Sopenharmony_ci ``` 47e41f4b71Sopenharmony_ci 48e41f4b71Sopenharmony_ci - Method 2: Obtain the current location.<br> 49e41f4b71Sopenharmony_ci Instantiate a [SingleLocationRequest](../../reference/apis-location-kit/js-apis-geoLocationManager.md#singlelocationrequest12) object to notify the system of the type of location service to be provided for applications and the timeout interval for a single location.<br> 50e41f4b71Sopenharmony_ci - Set the location priority by using **LocatingPriority**.<br> 51e41f4b71Sopenharmony_ci If a precise location is required, set **LocatingPriority** to **PRIORITY_ACCURACY**. The most accurate location result within the specified period of time will be provided for the application.<br> 52e41f4b71Sopenharmony_ci If fast location is expected, you are advised to set **LocatingPriority** to **PRIORITY_LOCATING_SPEED**. The first obtained location result will be provided for the application.<br> 53e41f4b71Sopenharmony_ci Both the GNSS positioning and the network positioning technologies are used in the two location policies. This can lead to significant hardware resource consumption and power consumption.<br> 54e41f4b71Sopenharmony_ci - Set the location timeout interval by using **locatingTimeoutMs**.<br> 55e41f4b71Sopenharmony_ci Due to the impact of the ambient environment, device status, and system power consumption control policy, the location response delay may fluctuates greatly. You are advised to set the timeout interval for a single location to 10 seconds.<br> 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ci The following code snippet uses **PRIORITY_LOCATING_SPEED** as an example.<br> 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ci ```ts 60e41f4b71Sopenharmony_ci import { geoLocationManager } from '@kit.LocationKit'; 61e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit' 62e41f4b71Sopenharmony_ci let request: geoLocationManager.SingleLocationRequest = { 63e41f4b71Sopenharmony_ci 'locatingPriority': geoLocationManager.LocatingPriority.PRIORITY_LOCATING_SPEED, 64e41f4b71Sopenharmony_ci 'locatingTimeoutMs': 10000 65e41f4b71Sopenharmony_ci } 66e41f4b71Sopenharmony_ci try { 67e41f4b71Sopenharmony_ci geoLocationManager.getCurrentLocation(request).then((result) => { // Call getCurrentLocation to obtain the current device location by using a promise. 68e41f4b71Sopenharmony_ci console.log('current location: ' + JSON.stringify(result)); 69e41f4b71Sopenharmony_ci }) 70e41f4b71Sopenharmony_ci .catch((error:BusinessError) => { // Receive the reported error code. 71e41f4b71Sopenharmony_ci console.error('promise, getCurrentLocation: error=' + JSON.stringify(error)); 72e41f4b71Sopenharmony_ci }); 73e41f4b71Sopenharmony_ci } catch (err) { 74e41f4b71Sopenharmony_ci console.error("errCode:" + JSON.stringify(err)); 75e41f4b71Sopenharmony_ci } 76e41f4b71Sopenharmony_ci ``` 77e41f4b71Sopenharmony_ci4. Obtain the device location continuously. It is mainly used in scenarios such as navigation, movement track, and travel.<br> 78e41f4b71Sopenharmony_ci Instantiate a [ContinuousLocationRequest](../../reference/apis-location-kit/js-apis-geoLocationManager.md#continuouslocationrequest12) object to notify the system of the type of location service to be provided for applications and the interval for reporting location information.<br> 79e41f4b71Sopenharmony_ci - Set the location scenario by using **locationScenario**.<br> 80e41f4b71Sopenharmony_ci You are advised to set **locationScenario** based on the application scenario. For details about the enum values of this parameter, see [UserActivityScenario](../../reference/apis-location-kit/js-apis-geoLocationManager.md#useractivityscenario12). For example, if **locationScenario** is set to **NAVIGATION**, the application will obtain the device location in both the indoor and outdoor scenarios for navigation.<br> 81e41f4b71Sopenharmony_ci - Set the interval for reporting location information by using **interval**.<br> 82e41f4b71Sopenharmony_ci The interval for reporting location information is in unit of seconds. The default value is **1**. Leave this parameter unspecified if there is no special requirement for the location reporting interval. 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci The following code snippet uses the map navigation scenario as an example. 85e41f4b71Sopenharmony_ci 86e41f4b71Sopenharmony_ci ```ts 87e41f4b71Sopenharmony_ci import { geoLocationManager } from '@kit.LocationKit'; 88e41f4b71Sopenharmony_ci let request: geoLocationManager.ContinuousLocationRequest= { 89e41f4b71Sopenharmony_ci 'interval': 1, 90e41f4b71Sopenharmony_ci 'locationScenario': geoLocationManager.UserActivityScenario.NAVIGATION 91e41f4b71Sopenharmony_ci } 92e41f4b71Sopenharmony_ci let locationCallback = (location:geoLocationManager.Location):void => { 93e41f4b71Sopenharmony_ci console.log('locationCallback: data: ' + JSON.stringify(location)); 94e41f4b71Sopenharmony_ci }; 95e41f4b71Sopenharmony_ci try { 96e41f4b71Sopenharmony_ci geoLocationManager.on('locationChange', request, locationCallback); 97e41f4b71Sopenharmony_ci } catch (err) { 98e41f4b71Sopenharmony_ci console.error("errCode:" + JSON.stringify(err)); 99e41f4b71Sopenharmony_ci } 100e41f4b71Sopenharmony_ci ``` 101e41f4b71Sopenharmony_ci If your application no longer needs the device location, stop obtaining the device location to avoid high power consumption. 102e41f4b71Sopenharmony_ci ```ts 103e41f4b71Sopenharmony_ci geoLocationManager.off('locationChange', locationCallback); 104e41f4b71Sopenharmony_ci ``` 105