openharmony 鸿蒙 js-apis-geoLocationManager-sys

2025-06-12 浏览 (1)

@ohos.geoLocationManager (Geolocation Manager) (System API)

The geoLocationManager module provides location services such as Global Navigation Satellite System (GNSS)-based positioning, network positioning, geofencing, as well as geocoding and reverse geocoding.

NOTE

The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version. This topic describes only system APIs provided by the module. For details about its public APIs, see @ohos.geoLocationManager (Geolocation Manager). This module supports only the WGS-84 coordinate system.

Applying for Permissions

Before using basic location capabilities, check whether your application has been granted the permission to access the device location information. If not, your application needs to obtain the permission from the user as described below.

The system provides the following location permissions:

  • ohos.permission.LOCATION

  • ohos.permission.APPROXIMATELY_LOCATION

  • ohos.permission.LOCATION_IN_BACKGROUND

If your application needs to access the device location information, it must first apply for required permissions. Specifically speaking:

API versions earlier than 9: Apply for ohos.permission.LOCATION.

API version 9 and later: Apply for ohos.permission.APPROXIMATELY_LOCATION, or apply for ohos.permission.APPROXIMATELY_LOCATION and ohos.permission.LOCATION. Note that ohos.permission.LOCATION cannot be applied for separately.

API VersionLocation PermissionPermission Application ResultLocation Accuracy
Earlier than 9ohos.permission.LOCATIONSuccessLocation accurate to meters
9 and laterohos.permission.LOCATIONFailureNo location obtained
9 and laterohos.permission.APPROXIMATELY_LOCATIONSuccessLocation accurate to 5 kilometers
9 and laterohos.permission.APPROXIMATELY_LOCATION and ohos.permission.LOCATIONSuccessLocation accurate to meters

To access the device location information when running in the background, an application needs to request for a continuous task of the LOCATION type. In this way, the system continues to report device location information after your application moves to the background. For details about how to request for a continuous task, see Continuous Task.

A user can grant the ohos.permission.LOCATION_IN_BACKGROUND permission for an application on the setting page. For details, see ohos.permission.LOCATION_IN_BACKGROUND.

You can declare the required permission in your application's configuration file. For details, see Requesting User Authorization.

Modules to Import

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

GeoAddress

Geocoding address information.

System capability: SystemCapability.Location.Location.Geocoder

NameTypeRead OnlyOptionalDescription
isFromMockBooleanNoYestrue: The geographic address is obtained from the mock reverse geocoding function.
false: The geographic address is not obtained from the mock reverse geocoding function.
System API: This is a system API.

Location

Location information.

System capability: SystemCapability.Location.Location.Core

NameTypeRead OnlyOptionalDescription
isFromMockBooleanNoYestrue: The location information is obtained from the mock location switch.
false: The location information is not obtained from the location simulation function.
System API: This is a system API.

ReverseGeocodingMockInfo

Defines the configuration of the mock reverse geocoding function.

System capability: SystemCapability.Location.Location.Core

System API: This is a system API.

NameTypeRead OnlyOptionalDescription
locationReverseGeoCodeRequestNoNoLatitude and longitude information.
geoAddressGeoAddressNoNoGeographical name.

LocationMockConfig

Defines the configuration of the mock location switch.

System capability: SystemCapability.Location.Location.Core

System API: This is a system API.

NameTypeRead OnlyOptionalDescription
timeIntervalnumberNoNoTime interval at which mock locations are reported, in seconds.
locationsArray<Location>NoNoArray of mocked locations.

LocatingRequiredDataConfig10+

Defines the configuration for obtaining the required data of the location service.

System capability: SystemCapability.Location.Location.Core

System API: This is a system API.

NameTypeRead OnlyOptionalDescription
typeLocatingRequiredDataTypeNoNoType of the required data.
needStartScanbooleanNoNotrue: Scanning needs to be initiated.
false: Scanning does not need to be initiated.
scanIntervalnumberNoYesScanning interval, in milliseconds. The specified value must be greater than 0. The default value is 10000.
scanTimeoutnumberNoYesScanning timeout interval, in milliseconds. The value ranges from 0 to 600000. The default value is 10000.

LocatingRequiredData10+

Defines the required data of the location service, including the Wi-Fi or Bluetooth scanning result. After obtaining the data, an application can use the data for services such as network positioning.

System capability: SystemCapability.Location.Location.Core

System API: This is a system API.

NameTypeRead OnlyOptionalDescription
wifiDataWifiScanInfoNoYesWi-Fi scanning result.
bluetoothDataBluetoothScanInfoNoYesBluetooth scanning result.

WifiScanInfo10+

Defines the Wi-Fi scanning information, including the SSID, BSSID, and RSSI of the scanned Wi-Fi hotspot.

System capability: SystemCapability.Location.Location.Core

System API: This is a system API.

NameTypeRead OnlyOptionalDescription
ssidstringNoNoService set identifier (SSID) of a Wi-Fi hotspot, in UTF-8 format.
bssidstringNoNoBase station subsystem identifier (BSSID) of a Wi-Fi hotspot.
rssinumberNoNoReceived signal strength indicator (RSSI) of a Wi-Fi hotspot, in dBm.
frequencynumberNoNoFrequency of a Wi-Fi hotspot.
timestampnumberNoNoScanning timestamp.

BluetoothScanInfo10+

Defines the Bluetooth scanning information.

System capability: SystemCapability.Location.Location.Core

System API: This is a system API.

NameTypeRead OnlyOptionalDescription
deviceNamestringNoNoName of a Bluetooth device.
macAddressstringNoNoMAC address of a Bluetooth device.
rssinumberNoNoSignal strength of a Bluetooth device, in dBm.
timestampnumberNoNoScanning timestamp.

LocationPrivacyType

Defines the privacy statement type.

System capability: SystemCapability.Location.Location.Core

System API: This is a system API.

NameValueDescription
OTHERS0Other scenarios. Reserved field.
STARTUP1Privacy statement displayed in the startup wizard. The user needs to choose whether to agree with the statement.
CORE_LOCATION2Privacy statement displayed when enabling the location service.

LocatingRequiredDataType10+

Defines the type of the required data of the location service.

System capability: SystemCapability.Location.Location.Core

System API: This is a system API.

NameValueDescription
WIFI1Wi-Fi scanning information.
BLUETOOTH2Bluetooth scanning information.

LocationIconStatus12+

Defines the location icon status.

System capability: SystemCapability.Location.Location.Core

System API: This is a system API.

NameValueDescription
LOCATING_NOT_STARTED0No location services are available and the location icon does not need to be displayed.
LOCATING_STARTED1Common location is in progress, and the common location icon needs to be displayed.
HD_LOCATING_STARTED2High-precision location is in progress, and the high-precision location icon needs to be displayed.

geoLocationManager.on('locatingRequiredDataChange')10+

on(type: 'locatingRequiredDataChange', config: LocatingRequiredDataConfig, callback: Callback<Array<LocatingRequiredData>>): void

Subscribes to changes in the required data of the location service, including Wi-Fi and Bluetooth scanning information. An application can then determine whether to enable Wi-Fi and Bluetooth scanning based on the return result. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.LOCATION and ohos.permission.APPROXIMATELY_LOCATION

System capability: SystemCapability.Location.Location.Core

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value locatingRequiredDataChange indicates a change in the required data of the location service.
configLocatingRequiredDataConfigYesConfiguration for obtaining the required data of the location service.
callbackCallback<Array<LocatingRequiredData>>YesCallback used to return the data required by the location service.

Error codes

For details about the error codes, see [Location Error Codes]](errorcode-geoLocationManager.md).

IDError Message
201Permission verification failed. The application does not have the permission required to call the API.
202Permission verification failed. A non-system application calls a system API.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
801Capability not supported. Failed to call ${geoLocationManager.on('locatingRequiredDataChange')} due to limited device capabilities.
3301800Failed to start WiFi or Bluetooth scanning.

Example

import { geoLocationManager } from '@kit.LocationKit';
let callback = (code:Array<geoLocationManager.LocatingRequiredData>):void => {
    console.info('locatingRequiredDataChange: ' + JSON.stringify(code));
}
let config:geoLocationManager.LocatingRequiredDataConfig = {'type': 1, 'needStartScan': true, 'scanInterval': 10000};
try {
    geoLocationManager.on('locatingRequiredDataChange', config, callback);
} catch (err) {
    console.error("errCode:" + err.code + ", message:"  + err.message);
}

geoLocationManager.off('locatingRequiredDataChange')10+

off(type: 'locatingRequiredDataChange', callback?: Callback<Array<LocatingRequiredData>>): void

Unsubscribes from changes in the required data of the location service and stops Wi-Fi and Bluetooth scanning.

Required permissions: ohos.permission.LOCATION and ohos.permission.APPROXIMATELY_LOCATION

System capability: SystemCapability.Location.Location.Core

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value locatingRequiredDataChange indicates a change in the required data of the location service.
callbackCallback<Array<LocatingRequiredData>>NoCallback to unregister. The callback must be the same as that passed by the on API. If this parameter is not specified, all callbacks of the specified event type are unregistered.

Error codes

For details about the error codes, see [Location Error Codes]](errorcode-geoLocationManager.md).

IDError Message
201Permission verification failed. The application does not have the permission required to call the API.
202Permission verification failed. A non-system application calls a system API.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
801Capability not supported. Failed to call ${geoLocationManager.off('locatingRequiredDataChange')} due to limited device capabilities.

Example

import { geoLocationManager } from '@kit.LocationKit';
let callback = (code:Array<geoLocationManager.LocatingRequiredData>):void => {
    console.info('locatingRequiredDataChange: ' + JSON.stringify(code));
}
let config:geoLocationManager.LocatingRequiredDataConfig = {'type': 1, 'needStartScan': true, 'scanInterval': 10000};
try {
    geoLocationManager.on('locatingRequiredDataChange', config, callback);
    geoLocationManager.off('locatingRequiredDataChange', callback);
} catch (err) {
    console.error("errCode:" + err.code + ", message:"  + err.message);
}

geoLocationManager.enableLocation

enableLocation(callback: AsyncCallback<void>): void

Enables the location service. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permissions: ohos.permission.MANAGE_SECURE_SETTINGS and ohos.permission.CONTROL_LOCATION_SWITCH

System capability: SystemCapability.Location.Location.Core

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<void>YesCallback used to return the result. If the location service is successfully enabled, err is undefined. Otherwise, err is an error object.

Error codes

For details about the error codes, see [Location Error Codes]](errorcode-geoLocationManager.md).

IDError Message
201Permission verification failed. The application does not have the permission required to call the API.
202Permission verification failed. A non-system application calls a system API.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
801Capability not supported. Failed to call ${geoLocationManager.enableLocation} due to limited device capabilities.
3301000The location service is unavailable.

Example

import { geoLocationManager } from '@kit.LocationKit';
try {
    geoLocationManager.enableLocation((err) => {
        if (err) {
            console.error('enableLocation: err=' + JSON.stringify(err));
        }
    });
} catch (err) {
    console.error("errCode:" + err.code + ", message:"  + err.message);
}

geoLocationManager.enableLocation

enableLocation(): Promise<void>

Enables the location service. This API uses a promise to return the result.

System API: This is a system API.

Required permissions: ohos.permission.MANAGE_SECURE_SETTINGS and ohos.permission.CONTROL_LOCATION_SWITCH

System capability: SystemCapability.Location.Location.Core

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see [Location Error Codes]](errorcode-geoLocationManager.md).

IDError Message
201Permission verification failed. The application does not have the permission required to call the API.
202Permission verification failed. A non-system application calls a system API.
801Capability not supported. Failed to call ${geoLocationManager.enableLocation} due to limited device capabilities.
3301000The location service is unavailable.

Example

import { geoLocationManager } from '@kit.LocationKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
    geoLocationManager.enableLocation().then(() => {
        console.info('promise, enableLocation succeed');
    })
    .catch((error:BusinessError) => {
        console.error('promise, enableLocation: error=' + JSON.stringify(error));
    });
} catch (err) {
    console.error("errCode:" + err.code + ", message:"  + err.message);
}

geoLocationManager.disableLocation

disableLocation(): void

Disables the location service.

System API: This is a system API.

Required permissions: ohos.permission.MANAGE_SECURE_SETTINGS and ohos.permission.CONTROL_LOCATION_SWITCH

System capability: SystemCapability.Location.Location.Core

Error codes

For details about the error codes, see [Location Error Codes]](errorcode-geoLocationManager.md).

IDError Message
201Permission verification failed. The application does not have the permission required to call the API.
202Permission verification failed. A non-system application calls a system API.
801Capability not supported. Failed to call ${geoLocationManager.disableLocation} due to limited device capabilities.
3301000The location service is unavailable.

Example

import { geoLocationManager } from '@kit.LocationKit';
try {
    geoLocationManager.disableLocation();
} catch (err) {
    console.error("errCode:" + err.code + ", message:"  + err.message);
}

geoLocationManager.enableLocationMock

enableLocationMock(): void

Enables the mock location switch.

System capability: SystemCapability.Location.Location.Core

System API: This is a system API.

Required permissions: ohos.permission.MOCK_LOCATION

Error codes

For details about the error codes, see [Location Error Codes]](errorcode-geoLocationManager.md).

IDError Message
201Permission verification failed. The application does not have the permission required to call the API.
202Permission verification failed. A non-system application calls a system API.
801Capability not supported. Failed to call ${geoLocationManager.enableLocationMock} due to limited device capabilities.
3301000The location service is unavailable.
3301100The location switch is off.

Example

import { geoLocationManager } from '@kit.LocationKit';
try {
    geoLocationManager.enableLocationMock();
} catch (err) {
    console.error("errCode:" + err.code + ", message:"  + err.message);
}

geoLocationManager.disableLocationMock

disableLocationMock(): void

Disables the mock location switch.

System capability: SystemCapability.Location.Location.Core

System API: This is a system API.

Required permissions: ohos.permission.MOCK_LOCATION

Error codes

For details about the error codes, see [Location Error Codes]](errorcode-geoLocationManager.md).

IDError Message
201Permission verification failed. The application does not have the permission required to call the API.
202Permission verification failed. A non-system application calls a system API.
801Capability not supported. Failed to call ${geoLocationManager.disableLocationMock} due to limited device capabilities.
3301000The location service is unavailable.
3301100The location switch is off.

Example

import { geoLocationManager } from '@kit.LocationKit';
try {
    geoLocationManager.disableLocationMock();
} catch (err) {
    console.error("errCode:" + err.code + ", message:"  + err.message);
}

geoLocationManager.setMockedLocations

setMockedLocations(config: LocationMockConfig): void

Sets the mock location information. The mock locations will be reported at the interval specified in this API.

This API can be invoked only after geoLocationManager.enableLocationMock is called.

System capability: SystemCapability.Location.Location.Core

System API: This is a system API.

Required permissions: ohos.permission.MOCK_LOCATION

Parameters

NameTypeMandatoryDescription
configLocationMockConfigYesMock location information, including the interval for reporting the mock locations and the array of the mock locations.

Error codes

For details about the error codes, see [Location Error Codes]](errorcode-geoLocationManager.md).

IDError Message
201Permission verification failed. The application does not have the permission required to call the API.
202Permission verification failed. A non-system application calls a system API.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
801Capability not supported. Failed to call ${geoLocationManager.setMockedLocations} due to limited device capabilities.
3301000The location service is unavailable.
3301100The location switch is off.

Example

import { geoLocationManager } from '@kit.LocationKit';
let locations:Array<geoLocationManager.Location> = [
    {"latitude": 30.12, "longitude": 120.11, "altitude": 123, "accuracy": 1, "speed": 5.2, "timeStamp": 16594326109, "direction": 123.11, "timeSinceBoot": 1000000000, "additionSize": 0, "isFromMock": true},
    {"latitude": 31.13, "longitude": 121.11, "altitude": 123, "accuracy": 2, "speed": 5.2, "timeStamp": 16594326109, "direction": 123.11, "timeSinceBoot": 2000000000, "additionSize": 0, "isFromMock": true},
    {"latitude": 32.14, "longitude": 122.11, "altitude": 123, "accuracy": 3, "speed": 5.2, "timeStamp": 16594326109, "direction": 123.11, "timeSinceBoot": 3000000000, "additionSize": 0, "isFromMock": true},
    {"latitude": 33.15, "longitude": 123.11, "altitude": 123, "accuracy": 4, "speed": 5.2, "timeStamp": 16594326109, "direction": 123.11, "timeSinceBoot": 4000000000, "additionSize": 0, "isFromMock": true},
    {"latitude": 34.16, "longitude": 124.11, "altitude": 123, "accuracy": 5, "speed": 5.2, "timeStamp": 16594326109, "direction": 123.11, "timeSinceBoot": 5000000000, "additionSize": 0, "isFromMock": true}
];
let config:geoLocationManager.LocationMockConfig = {"timeInterval": 5, "locations": locations};
try {
    geoLocationManager.enableLocationMock();
    geoLocationManager.setMockedLocations(config);
} catch (err) {
    console.error("errCode:" + err.code + ", message:"  + err.message);
}

geoLocationManager.enableReverseGeocodingMock

enableReverseGeocodingMock(): void

Enables the mock reverse geocoding function.

System capability: SystemCapability.Location.Location.Core

System API: This is a system API.

Required permissions: ohos.permission.MOCK_LOCATION

Error codes

For details about the error codes, see [Location Error Codes]](errorcode-geoLocationManager.md).

IDError Message
201Permission verification failed. The application does not have the permission required to call the API.
202Permission verification failed. A non-system application calls a system API.
801Capability not supported. Failed to call ${geoLocationManager.enableReverseGeocodingMock} due to limited device capabilities.
3301000The location service is unavailable.

Example

import { geoLocationManager } from '@kit.LocationKit';
try {
    geoLocationManager.enableReverseGeocodingMock();
} catch (err) {
    console.error("errCode:" + err.code + ", message:"  + err.message);
}

geoLocationManager.disableReverseGeocodingMock

disableReverseGeocodingMock(): void

Disables the mock geocoding function.

System capability: SystemCapability.Location.Location.Core

System API: This is a system API.

Required permissions: ohos.permission.MOCK_LOCATION

Error codes

For details about the error codes, see [Location Error Codes]](errorcode-geoLocationManager.md).

IDError Message
201Permission verification failed. The application does not have the permission required to call the API.
202Permission verification failed. A non-system application calls a system API.
801Capability not supported. Failed to call ${geoLocationManager.disableReverseGeocodingMock} due to limited device capabilities.
3301000The location service is unavailable.

Example

import { geoLocationManager } from '@kit.LocationKit';
try {
    geoLocationManager.disableReverseGeocodingMock();
} catch (err) {
    console.error("errCode:" + err.code + ", message:"  + err.message);
}

geoLocationManager.setReverseGeocodingMockInfo

setReverseGeocodingMockInfo(mockInfos: Array<ReverseGeocodingMockInfo>): void

Sets information of the mock reverse geocoding function, including the mapping between a location and geographical name. If the location is contained in the configurations during reverse geocoding query, the corresponding geographical name will be returned.

This API can be invoked only after geoLocationManager.enableReverseGeocodingMock is called.

System capability: SystemCapability.Location.Location.Core

System API: This is a system API.

Required permissions: ohos.permission.MOCK_LOCATION

Parameters

NameTypeMandatoryDescription
mockInfosArray<ReverseGeocodingMockInfo>YesArray of information of the mock reverse geocoding function, including a location and a geographical name.

Error codes

For details about the error codes, see [Location Error Codes]](errorcode-geoLocationManager.md).

IDError Message
201Permission verification failed. The application does not have the permission required to call the API.
202Permission verification failed. A non-system application calls a system API.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
801Capability not supported. Failed to call ${geoLocationManager.setReverseGeocodingMockInfo} due to limited device capabilities.
3301000The location service is unavailable.

Example

import { geoLocationManager } from '@kit.LocationKit';
let mockInfos:Array<geoLocationManager.ReverseGeocodingMockInfo> = [
    {"location": {"locale": "zh", "latitude": 30.12, "longitude": 120.11, "maxItems": 1}, "geoAddress": {"locale": "zh", "latitude": 30.12, "longitude": 120.11, "isFromMock": true}},
    {"location": {"locale": "zh", "latitude": 31.12, "longitude": 121.11, "maxItems": 1}, "geoAddress": {"locale": "zh", "latitude": 31.12, "longitude": 121.11, "isFromMock": true}},
    {"location": {"locale": "zh", "latitude": 32.12, "longitude": 122.11, "maxItems": 1}, "geoAddress": {"locale": "zh", "latitude": 32.12, "longitude": 122.11, "isFromMock": true}},
    {"location": {"locale": "zh", "latitude": 33.12, "longitude": 123.11, "maxItems": 1}, "geoAddress": {"locale": "zh", "latitude": 33.12, "longitude": 123.11, "isFromMock": true}},
    {"location": {"locale": "zh", "latitude": 34.12, "longitude": 124.11, "maxItems": 1}, "geoAddress": {"locale": "zh", "latitude": 34.12, "longitude": 124.11, "isFromMock": true}},
];
try {
    geoLocationManager.enableReverseGeocodingMock();
    geoLocationManager.setReverseGeocodingMockInfo(mockInfos);
} catch (err) {
    console.error("errCode:" + err.code + ", message:"  + err.message);
}

geoLocationManager.isLocationPrivacyConfirmed

isLocationPrivacyConfirmed(type: LocationPrivacyType): boolean

Checks whether a user agrees with the privacy statement of the location service. This API can only be called by system applications.

System API: This is a system API.

System capability: SystemCapability.Location.Location.Core

Parameters

NameTypeMandatoryDescription
typeLocationPrivacyTypeYesPrivacy statement type, for example, privacy statement displayed in the startup wizard or privacy statement displayed when the location service is enabled.

Return value

TypeDescription
booleantrue: The user agrees to the privacy statement of the location service.
false: The user does not agree to the privacy statement of the location service.

Error codes

For details about the error codes, see [Location Error Codes]](errorcode-geoLocationManager.md).

IDError Message
202Permission verification failed. A non-system application calls a system API.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
801Capability not supported. Failed to call ${geoLocationManager.isLocationPrivacyConfirmed} due to limited device capabilities.
3301000The location service is unavailable.

Example

import { geoLocationManager } from '@kit.LocationKit';
try {
    let isConfirmed = geoLocationManager.isLocationPrivacyConfirmed(1);
} catch (err) {
    console.error("errCode:" + err.code + ", message:"  + err.message);
}

geoLocationManager.setLocationPrivacyConfirmStatus

setLocationPrivacyConfirmStatus(type: LocationPrivacyType, isConfirmed: boolean): void

Sets the user confirmation status for the privacy statement of the location service. This API can only be called by system applications.

System API: This is a system API.

Required permissions: ohos.permission.MANAGE_SECURE_SETTINGS

System capability: SystemCapability.Location.Location.Core

Parameters

NameTypeMandatoryDescription
typeLocationPrivacyTypeYesPrivacy statement type, for example, privacy statement displayed in the startup wizard or privacy statement displayed when the location service is enabled.
isConfirmedbooleanYestrue: The user agrees to the privacy statement of the location service.
false: The user does not agree to the privacy statement of the location service.

Error codes

For details about the error codes, see [Location Error Codes]](errorcode-geoLocationManager.md).

IDError Message
201Permission verification failed. The application does not have the permission required to call the API.
202Permission verification failed. A non-system application calls a system API.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
801Capability not supported. Failed to call ${geoLocationManager.setLocationPrivacyConfirmStatus} due to limited device capabilities.
3301000The location service is unavailable.

Example

import { geoLocationManager } from '@kit.LocationKit';
try {
    geoLocationManager.setLocationPrivacyConfirmStatus(1, true);
} catch (err) {
    console.error("errCode:" + err.code + ", message:"  + err.message);
}

geoLocationManager.getLocatingRequiredData10+

getLocatingRequiredData(config: LocatingRequiredDataConfig): Promise<Array<LocatingRequiredData>>

Obtains the required data of the location service. This API uses a promise to return the result.

Required permissions: ohos.permission.LOCATION and ohos.permission.APPROXIMATELY_LOCATION

System capability: SystemCapability.Location.Location.Core

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
configLocatingRequiredDataConfigYesConfiguration for obtaining the required data of the location service.

Return value

TypeDescription
Promise<Array<LocatingRequiredData>>Promise used to return the required data for the location service, such as the Wi-Fi and Bluetooth scanning information.

Error codes

For details about the error codes, see [Location Error Codes]](errorcode-geoLocationManager.md).

IDError Message
201Permission verification failed. The application does not have the permission required to call the API.
202Permission verification failed. A non-system application calls a system API.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
801Capability not supported. Failed to call ${geoLocationManager.getLocatingRequiredData} due to limited device capabilities.
3301800Failed to start WiFi or Bluetooth scanning.

Example

import { geoLocationManager } from '@kit.LocationKit';
import { BusinessError } from '@kit.BasicServicesKit';
let config:geoLocationManager.LocatingRequiredDataConfig = {'type': 1, 'needStartScan': true, 'scanInterval': 10000};
try {
    geoLocationManager.getLocatingRequiredData(config).then((result) => {
        console.info('getLocatingRequiredData return: ' + JSON.stringify(result));
    })  
    .catch((error:BusinessError) => {
        console.error('promise, getLocatingRequiredData: error=' + JSON.stringify(error));
    });
} catch (err) {
    console.error("errCode:" + err.code + ", message:"  + err.message);
}

geoLocationManager.on('locationIconStatusChange')12+

on(type: 'locationIconStatusChange', callback: Callback<LocationIconStatus>): void

Subscribes to status changes of the location icon. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Location.Location.Core

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. Event type. The value locationIconStatusChange indicates status changes of the location icon.
callbackCallback<LocationIconStatus>YesCallback used to return the location icon status.

Error codes

For details about the error codes, see [Location Error Codes]](errorcode-geoLocationManager.md).

IDError Message
202Permission verification failed. A non-system application calls a system API.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
801Capability not supported. Failed to call ${geoLocationManager.on('locationIconStatusChange')} due to limited device capabilities.
3301000The location service is unavailable.

Example

import { geoLocationManager } from '@kit.LocationKit';
let callback = (code: geoLocationManager.LocationIconStatus):void => {
    console.info('LocationIconStatus: ' + JSON.stringify(code));
}
try {
    geoLocationManager.on('locationIconStatusChange', callback);
} catch (err) {
    console.error("errCode:" + err.code + ", message:"  + err.message);
}

geoLocationManager.off('locationIconStatusChange')12+

off(type: 'locationIconStatusChange', callback?: Callback<LocationIconStatus>): void

Unsubscribes from status changes of the location icon.

System capability: SystemCapability.Location.Location.Core

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. Event type. The value locationIconStatusChange indicates status changes of the location icon.
callbackCallback<LocationIconStatus>NoCallback to unregister. The callback must be the same as that passed by the on API. If this parameter is not specified, all callbacks of the specified event type are unregistered.

Error codes

For details about the error codes, see [Location Error Codes]](errorcode-geoLocationManager.md).

IDError Message
202Permission verification failed. A non-system application calls a system API.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
801Capability not supported. Failed to call ${geoLocationManager.off('locationIconStatusChange')} due to limited device capabilities.
3301000The location service is unavailable.

Example

import { geoLocationManager } from '@kit.LocationKit';
let callback = (code: geoLocationManager.LocationIconStatus):void => {
    console.info('LocationIconStatus: ' + JSON.stringify(code));
}
try {
    geoLocationManager.on('locationIconStatusChange', callback);
    geoLocationManager.off('locationIconStatusChange', callback);
} catch (err) {
    console.error("errCode:" + err.code + ", message:"  + err.message);
}

geoLocationManager.getLocationIconStatus12+

getLocationIconStatus(): LocationIconStatus

Obtains the current location icon status.

System capability: SystemCapability.Location.Location.Core

System API: This is a system API.

Return value

TypeDescription
LocationIconStatusLocation icon status.

Error codes

For details about the error codes, see [Location Error Codes]](errorcode-geoLocationManager.md).

IDError Message
202Permission verification failed. A non-system application calls a system API.
801Capability not supported. Failed to call ${geoLocationManager.getLocationIconStatus} due to limited device capabilities.
3301000The location service is unavailable.

Example

import { geoLocationManager } from '@kit.LocationKit';
try {
    let iconStatus = geoLocationManager.getLocationIconStatus();
} catch (err) {
    console.error("errCode:" + err.code + ", message:"  + err.message);
}

geoLocationManager.enableLocationByUserId18+

enableLocationByUserId(userId: number): Promise<void>

Enables the location switch for the specified system account. This API uses a promise to return the result.

System API: This is a system API.

Required permissions: ohos.permission.MANAGE_SECURE_SETTINGS and ohos.permission.CONTROL_LOCATION_SWITCH

System capability: SystemCapability.Location.Location.Core

Parameters

NameTypeMandatoryDescription
userIdnumberYesSystem account ID. For details about how to obtain the system account ID, see Obtaining All System Accounts.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see [Location Error Codes]](errorcode-geoLocationManager.md).

IDError Message
201Permission verification failed. The application does not have the permission required to call the API.
202Permission verification failed. A non-system application calls a system API.
801Capability not supported. Failed to call ${geoLocationManager.enableLocationByUserId} due to limited device capabilities.
3301000The location service is unavailable.

Example

import { geoLocationManager } from '@kit.LocationKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
    // Enable the location switch for the specified system account. For example, if the account ID is below 101, you can enable the location switch for the account whose ID is 100.
    let userId:number = 100;
    geoLocationManager.enableLocationByUserId(userId).then(() => {
        console.info('promise, enableLocationByUserId succeed');
    })
    .catch((error:BusinessError) => {
        console.error('promise, enableLocationByUserId: error=' + JSON.stringify(error));
    });
} catch (err) {
    console.error("errCode:" + err.code + ", message:"  + err.message);
}

geoLocationManager.disableLocationByUserId18+

disableLocationByUserId(userId: number): void

Disables the location switch for the specified system account.

System API: This is a system API.

Required permissions: ohos.permission.MANAGE_SECURE_SETTINGS and ohos.permission.CONTROL_LOCATION_SWITCH

System capability: SystemCapability.Location.Location.Core

Parameters

NameTypeMandatoryDescription
userIdnumberYesSystem account ID. For details about how to obtain the system account ID, see Obtaining All System Accounts.

Error codes

For details about the error codes, see [Location Error Codes]](errorcode-geoLocationManager.md).

IDError Message
201Permission verification failed. The application does not have the permission required to call the API.
202Permission verification failed. A non-system application calls a system API.
801Capability not supported. Failed to call ${geoLocationManager.disableLocationByUserId} due to limited device capabilities.
3301000The location service is unavailable.

Example

import { geoLocationManager } from '@kit.LocationKit';
try {
    // Disable the location switch for the specified system account. For example, if the account ID is below 101, you can disable the location switch for the account whose ID is 100.
    let userId:number = 100;
    geoLocationManager.disableLocationByUserId(userId);
} catch (err) {
    console.error("errCode:" + err.code + ", message:"  + err.message);
}

geoLocationManager.isLocationEnabledByUserId18+

isLocationEnabledByUserId(userId: number): boolean

Checks whether the location switch is enabled for the specified system account.

System API: This is a system API.

System capability: SystemCapability.Location.Location.Core

Parameters

NameTypeMandatoryDescription
userIdnumberYesSystem account ID. For details about how to obtain the system account ID, see Obtaining All System Accounts.

Return value

TypeDescription
booleantrue: The location switch is enabled.
false: The location switch is disabled.

Error codes

For details about the error codes, see [Location Error Codes]](errorcode-geoLocationManager.md).

IDError Message
202Permission verification failed. A non-system application calls a system API.
801Capability not supported. Failed to call ${geoLocationManager.isLocationEnabled} due to limited device capabilities.
3301000The location service is unavailable.

Example

import { geoLocationManager } from '@kit.LocationKit';
try {
    // Check whether the location switch is enabled for the specified system account. For example, if the account ID is below 101, you can check whether the location switch is enabled for the account whose ID is 100.
    let userId:number = 100;
    let locationEnabled = geoLocationManager.isLocationEnabledByUserId(userId);
} catch (err) {
    console.error("errCode:" + err.code + ", message:"  + err.message);
}

geoLocationManager.setLocationSwitchIgnored18+

setLocationSwitchIgnored(isIgnored: boolean): void

Sets whether the location switch is ignored.

If this parameter is set to true, the application can obtain the location information when the location switch is disabled. The setting takes effect 2 minutes after the API is successfully called.

System API: This is a system API.

Required permissions: ohos.permission.LOCATION_SWITCH_IGNORED

System capability: SystemCapability.Location.Location.Core

Parameters

NameTypeMandatoryDescription
isIgnoredbooleanYestrue: The application can obtain location information when the location switch is disabled. The setting takes effect 2 minutes after the API is successfully called.
false: The application cannot obtain location information when the location switch is disabled.

Error codes

For details about the error codes, see [Location Error Codes]](errorcode-geoLocationManager.md).

IDError Message
201Permission verification failed. The application does not have the permission required to call the API.
202Permission verification failed. A non-system application calls a system API.
801Capability not supported. Failed to call ${geoLocationManager.disableLocationByUserId} due to limited device capabilities.
3301000The location service is unavailable.

Example

import { geoLocationManager } from '@kit.LocationKit';
try {
    let isIgnored:boolean = true;
    geoLocationManager.setLocationSwitchIgnored(isIgnored);
} catch (err) {
    console.error("errCode:" + err.code + ", message:"  + err.message);
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Location Kit

harmony 鸿蒙Location

harmony 鸿蒙Location_BasicInfo

harmony 鸿蒙Location Kit Error Codes

harmony 鸿蒙@ohos.app.ability.FenceExtensionAbility (FenceExtensionAbility)

harmony 鸿蒙@ohos.app.ability.FenceExtensionContext (FenceExtensionContext) (System API)

harmony 鸿蒙@ohos.app.ability.FenceExtensionContext (FenceExtensionContext)

harmony 鸿蒙@ohos.geoLocationManager (Geolocation Manager)

harmony 鸿蒙@ohos.geolocation (Geolocation)

harmony 鸿蒙@system.geolocation (Geolocation)

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/A5CIkX