In real-world app development, mobile device location information is frequently required for scenarios such as local weather inquiry, ride hailing, travel navigation, and movement path tracking. Location Kit provides two positioning methods: GNSS positioning and network positioning, as described in the following table.
Positioning Method | Description | Advantage |
|---|---|---|
GNSS positioning | Locates a mobile device by using the location algorithm offered by the device chip to compute the location information provided by the Global Navigation Satellite System, for example, GPS, GLONASS, BeiDou, and Galileo. | Precise positioning |
Network positioning | Locates a device through WLAN, Bluetooth, or base station positioning. | Fast positioning |
By leveraging the system's location positioning capabilities, you can obtain real-time and accurate location information across various development scenarios. This document will introduce four common positioning scenarios and provide their specific implementation solutions to help you better understand the basic principles and development processes of location positioning.
You can set appropriate positioning policies to obtain the device's current location information based on actual service requirements. Different positioning policies correspond to different positioning methods described in Table 1.
How to Implement
Location Kit provides the getCurrentLocation() API to obtain current location information. This API requires users to set key parameters within the location request options, including positioning priority and single positioning timeout. Location request information is divided into two types: CurrentLocationRequest and SingleLocationRequest. The positioning priorities corresponding to these two types are LocationRequestPriority and LocatingPriority.
How to Develop
let request: geoLocationManager.SingleLocationRequest = {
locatingPriority: 0x502,
locatingTimeoutMs: 10000
}; geoLocationManager.getCurrentLocation(request).then((location: geoLocationManager.Location) => {
// ...
}).catch((err: BusinessError) => {
hilog.error(0x0000, TAG, `getCurrentLocationPosition failed, code: ${err.code}, message: ${err.message}`);
// ...
}); geoLocationManager.getAddressesFromLocation(reverseGeocodeRequest, async (err, data) => {
if (data) {
this.address = data[0]?.placeName || '';
// ...
} else {
hilog.error(0x0000, TAG, `getAddressesFromLocation failed, code: ${err.code}, message: ${err.message}`);
// ...
}
}); 
You can set appropriate positioning policies to continuously obtain device location information based on actual user activity scenarios or power consumption scenarios. Different positioning policies correspond to different positioning methods described in Table 1.
How to Implement
You can subscribe to location changes through the on('locationChange') API to continuously obtain the device location information. Before that, you need to instantiate a LocationRequest or ContinuousLocationRequest object, and set an appropriate positioning scenario and location information reporting interval.
How to Develop
let request: geoLocationManager.ContinuousLocationRequest = {
interval: 1,
locationScenario: 0x401
}; geoLocationManager.on('locationChange', request, this.locationChange); geoLocationManager.getAddressesFromLocation(reverseGeocodeRequest, async (err, data) => {
if (data) {
this.address = data[0]?.placeName || '';
// ...
} else {
hilog.error(0x0000, TAG, `getAddressesFromLocation failed, code: ${err.code}, message: ${err.message}`);
// ...
}
}); try {
geoLocationManager.off('locationChange', this.locationChange);
} catch (err) {
hilog.error(0x0000, TAG, `offLocationChange failed, code: ${err.code}, message: ${err.message}`);
} 

When an app switches to the background and still needs to obtain device location information, you can use this method for background location positioning.
How to Implement
Background location positioning requires the ohos.permission.LOCATION_IN_BACKGROUND permission for the app to access location in the background and the ohos.permission.KEEP_BACKGROUND_RUNNING permission for the application to run in the background. After obtaining the required permissions, start a continuous task with BackgroundMode set to LOCATION, and call on('locationChange') in its callback function to subscribe to location changes. This way, the application continuously obtains the current location information while running in the background.
How to Develop
Apply for the continuous task permission.
{
"name": "ohos.permission.KEEP_BACKGROUND_RUNNING",
"reason": "$string:running_background",
"usedScene": {
"abilities": [
"EntryAbility"
],
"when": "always"
}
}, Set BackgroundMode of the continuous task to LOCATION.
"abilities": [
{
// ...
"backgroundModes": [
"location"
],
// ...
}
], Sample code for starting a continuous task:
startContinuousTask(): void {
let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
if (!context) {
return;
}
let wantAgentInfo: wantAgent.WantAgentInfo = {
wants: [
{
bundleName: context.abilityInfo.bundleName,
abilityName: context.abilityInfo.name
}
],
operationType: wantAgent.OperationType.START_ABILITY,
requestCode: 1,
wantAgentFlags: [wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
};
wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj) => {
backgroundTaskManager.startBackgroundRunning(context,
backgroundTaskManager.BackgroundMode.LOCATION, wantAgentObj).then(() => {
this.onLocationChange();
hilog.info(0x0000, TAG, 'startBackgroundRunning succeeded');
}).catch((err: BusinessError) => {
hilog.error(0x0000, TAG, `startBackgroundRunning failed, cause: ${JSON.stringify(err)}`);
});
}).catch((err: BusinessError) => {
hilog.error(0x0000, TAG, `getWantAgent failed, cause: ${JSON.stringify(err)}`);
});
} Sample code for subscribing to location changes:
onLocationChange(): void {
let request: geoLocationManager.ContinuousLocationRequest = {
interval: 1,
locationScenario: 0x401
};
try {
geoLocationManager.on('locationChange', request, this.locationChange);
// ...
} catch (err) {
hilog.error(0x0000, TAG, `onLocationChange failed, code: ${err.code}, message: ${err.message}`);
// ...
}
} geoLocationManager.getAddressesFromLocation(reverseGeocodeRequest, async (err, data) => {
if (data) {
this.address = data[0]?.placeName || '';
// ...
} else {
hilog.error(0x0000, TAG, `getAddressesFromLocation failed, code: ${err.code}, message: ${err.message}`);
// ...
}
}); Sample code for closing a continuous task
stopContinuousTask(): void {
let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
if (!context) {
return;
}
backgroundTaskManager.stopBackgroundRunning(context).then(() => {
if (!this.isOnLocationChange) {
this.offLocationChange();
}
hilog.info(0x0000, TAG, 'stopBackgroundRunning succeeded');
}).catch((err: BusinessError) => {
hilog.error(0x0000, TAG, `stopBackgroundRunning failed, cause: ${JSON.stringify(err)}`);
});
} Sample code for unsubscribing from location changes:
offLocationChange(): void {
try {
geoLocationManager.off('locationChange', this.locationChange);
} catch (err) {
hilog.error(0x0000, TAG, `offLocationChange failed, code: ${err.code}, message: ${err.message}`);
}
} 

When the device's network signal is weak or when system power consumption is a concern, you can first obtain the system's cached latest location, which is the most recent historical location information.
How to Implement
Location Kit uses getLastLocation() to obtain the latest location information cached in the system. If the parameter list of this API is empty, and the return value is the location information defined by Location.
How to Develop
let location = geoLocationManager.getLastLocation();
geoLocationManager.getAddressesFromLocation(reverseGeocodeRequest, async (err, data) => {
if (data) {
this.address = data[0]?.placeName || '';
// ...
} else {
hilog.error(0x0000, TAG, `getAddressesFromLocation failed, code: ${err.code}, message: ${err.message}`);
// ...
}
}); 
Symptom
During location positioning, the location results obtained are inaccurate, or there are deviations when marking the location results on the map.
Possible Cause
Solution
Symptom
Location information cannot be obtained using the location function.
Possible Cause
Solution
Symptom
After getCurrentLocation() is invoked to obtain the current location information and then getLastLocation() is invoked to obtain the cached location information, the obtained location information is inconsistent.
Possible Cause
All apps share the same cached location information in the system. It is possible that another app initiates location between two API calls, which leads to updating of the cached location information.
Solution
Compare the time when the location information is obtained and determine whether the cached location information has been updated.