# Geolocation
Location services provide basic functions such as GNSS positioning, network positioning, geocoding, reverse geocoding, country code and geofencing.
>  **NOTE**
> The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.
## Modules to Import
```js
import geolocation from '@ohos.geolocation';
```
## geolocation.on('locationChange')
on(type: 'locationChange', request: LocationRequest, callback: Callback<Location>) : void
Registers a listener for location changes with a location request initiated.
**Permission required**: ohos.permission.LOCATION
**System capability**: SystemCapability.Location.Location.Core
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Event type. The value **locationChange** indicates a location change event.|
| request | LocationRequest | Yes| Location request.|
| callback | Callback<[Location](#location)> | Yes| Callback used to return the location change event.|
**Example**
```js
var requestInfo = {'priority': 0x203, 'scenario': 0x300, 'timeInterval': 0, 'distanceInterval': 0, 'maxAccuracy': 0};
var locationChange = (location) => {
console.log('locationChanger: data: ' + JSON.stringify(location));
};
geolocation.on('locationChange', requestInfo, locationChange);
```
## geolocation.off('locationChange')
off(type: 'locationChange', callback?: Callback<Location>) : void
Unregisters the listener for location changes with the corresponding location request deleted.
**Permission required**: ohos.permission.LOCATION
**System capability**: SystemCapability.Location.Location.Core
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Event type. The value **locationChange** indicates a location change event.|
| callback | Callback<[Location](#location)> | No| Callback used to return the location change event.|
**Example**
```js
var requestInfo = {'priority': 0x203, 'scenario': 0x300, 'timeInterval': 0, 'distanceInterval': 0, 'maxAccuracy': 0};
var locationChange = (location) => {
console.log('locationChanger: data: ' + JSON.stringify(location));
};
geolocation.on('locationChange', requestInfo, locationChange);
geolocation.off('locationChange', locationChange);
```
## geolocation.on('locationServiceState')
on(type: 'locationServiceState', callback: Callback<boolean>) : void
Registers a listener for location service status change events.
**Permission required**: ohos.permission.LOCATION
**System capability**: SystemCapability.Location.Location.Core
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Event type. The value **locationServiceState** indicates a location service status change event.|
| callback | Callback<boolean> | Yes| Callback used to return the location service status change event.|
**Example**
```js
var locationServiceState = (state) => {
console.log('locationServiceState: ' + JSON.stringify(state));
}
geolocation.on('locationServiceState', locationServiceState);
```
## geolocation.off('locationServiceState')
off(type: 'locationServiceState', callback?: Callback<boolean>) : void;
Unregisters the listener for location service status change events.
**Permission required**: ohos.permission.LOCATION
**System capability**: SystemCapability.Location.Location.Core
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Event type. The value **locationServiceState** indicates a location service status change event.|
| callback | Callback<boolean> | No| Callback used to return the location service status change event.|
**Example**
```js
var locationServiceState = (state) => {
console.log('locationServiceState: state: ' + JSON.stringify(state));
}
geolocation.on('locationServiceState', locationServiceState);
geolocation.off('locationServiceState', locationServiceState);
```
## geolocation.on('cachedGnssLocationsReporting')8+
on(type: 'cachedGnssLocationsReporting', request: CachedGnssLocationsRequest, callback: Callback<Array<Location>>) : void;
Registers a listener for cached GNSS location reports.
**Permission required**: ohos.permission.LOCATION
**System capability**: SystemCapability.Location.Location.Gnss
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Event type. The value **cachedGnssLocationsReporting** indicates reporting of cached GNSS locations.|
| request | CachedGnssLocationsRequest | Yes| Request for reporting cached GNSS location.|
| callback | Callback<boolean> | Yes| Callback used to return cached GNSS locations.|
**Example**
```js
var cachedLocationsCb = (locations) => {
console.log('cachedGnssLocationsReporting: locations: ' + JSON.stringify(locations));
}
var requestInfo = {'reportingPeriodSec': 10, 'wakeUpCacheQueueFull': true};
geolocation.on('cachedGnssLocationsReporting', requestInfo, cachedLocationsCb);
```
## geolocation.off('cachedGnssLocationsReporting')8+
off(type: 'cachedGnssLocationsReporting', callback?: Callback<Array<Location>>) : void;
Unregisters the listener for cached GNSS location reports.
**Permission required**: ohos.permission.LOCATION
**System capability**: SystemCapability.Location.Location.Gnss
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Event type. The value **cachedGnssLocationsReporting** indicates reporting of cached GNSS locations.|
| callback | Callback<boolean> | No| Callback used to return cached GNSS locations.|
**Example**
```js
var cachedLocationsCb = (locations) => {
console.log('cachedGnssLocationsReporting: locations: ' + JSON.stringify(locations));
}
var requestInfo = {'reportingPeriodSec': 10, 'wakeUpCacheQueueFull': true};
geolocation.on('cachedGnssLocationsReporting', requestInfo, cachedLocationsCb);
geolocation.off('cachedGnssLocationsReporting');
```
## geolocation.on('gnssStatusChange')8+
on(type: 'gnssStatusChange', callback: Callback<SatelliteStatusInfo>) : void;
Registers a listener for GNSS satellite status change events.
**Permission required**: ohos.permission.LOCATION
**System capability**: SystemCapability.Location.Location.Gnss
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Event type. The value **gnssStatusChange** indicates a GNSS satellite status change.|
| callback | Callback<SatelliteStatusInfo> | Yes| Callback used to return GNSS satellite status changes.|
**Example**
```js
var gnssStatusCb = (satelliteStatusInfo) => {
console.log('gnssStatusChange: ' + JSON.stringify(satelliteStatusInfo));
}
geolocation.on('gnssStatusChange', gnssStatusCb);
```
## geolocation.off('gnssStatusChange')8+
off(type: 'gnssStatusChange', callback?: Callback<SatelliteStatusInfo>) : void;
Unregisters the listener for GNSS satellite status change events.
**Permission required**: ohos.permission.LOCATION
**System capability**: SystemCapability.Location.Location.Gnss
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Event type. The value **gnssStatusChange** indicates a GNSS satellite status change.|
| callback | Callback<SatelliteStatusInfo> | No| Callback used to return GNSS satellite status changes.|
**Example**
```js
var gnssStatusCb = (satelliteStatusInfo) => {
console.log('gnssStatusChange: ' + JSON.stringify(satelliteStatusInfo));
}
geolocation.on('gnssStatusChange', gnssStatusCb);
geolocation.off('gnssStatusChange', gnssStatusCb);
```
## geolocation.on('nmeaMessageChange')8+
on(type: 'nmeaMessageChange', callback: Callback<string>) : void;
Registers a listener for GNSS NMEA message change events.
**Permission required**: ohos.permission.LOCATION
**System capability**: SystemCapability.Location.Location.Gnss
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Event type. The value **nmeaMessageChange** indicates a GNSS NMEA message change.|
| callback | Callback<string> | Yes| Callback used to return GNSS NMEA message changes.|
**Example**
```js
var nmeaCb = (str) => {
console.log('nmeaMessageChange: ' + JSON.stringify(str));
}
geolocation.on('nmeaMessageChange', nmeaCb );
```
## geolocation.off('nmeaMessageChange')8+
off(type: 'nmeaMessageChange', callback?: Callback<string>) : void;
Unregisters the listener for GNSS NMEA message change events.
**Permission required**: ohos.permission.LOCATION
**System capability**: SystemCapability.Location.Location.Gnss
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Event type. The value **nmeaMessageChange** indicates a GNSS NMEA message change.|
| callback | Callback<string> | No| Callback used to return GNSS NMEA message changes.|
**Example**
```js
var nmeaCb = (str) => {
console.log('nmeaMessageChange: ' + JSON.stringify(str));
}
geolocation.on('nmeaMessageChange', nmeaCb);
geolocation.off('nmeaMessageChange', nmeaCb);
```
## geolocation.on('fenceStatusChange')8+
on(type: 'fenceStatusChange', request: GeofenceRequest, want: WantAgent) : void;
Registers a listener for status change events of the specified geofence.
**Permission required**: ohos.permission.LOCATION
**System capability**: SystemCapability.Location.Location.Geofence
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Event type. The value **fenceStatusChange** indicates a geofence status change.|
| request | GeofenceRequest | Yes| Geofencing request.|
| want | WantAgent | Yes| **WantAgent** used to return geofence (entrance or exit) events.|
**Example**
```js
import geolocation from '@ohos.geolocation';
import wantAgent from '@ohos.wantAgent';
let wantAgentInfo = {
wants: [
{
bundleName: "com.example.myapplication",
abilityName: "com.example.myapplication.MainAbility"
action: "action1",
}
],
operationType: wantAgent.OperationType.START_ABILITY,
requestCode: 0,
wantAgentFlags: [wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
};
wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj) => {
var requestInfo = {'priority': 0x201, 'scenario': 0x301, "geofence": {"latitude": 121, "longitude": 26, "radius": 100, "expiration": 10000}};
geolocation.on('fenceStatusChange', requestInfo, wantAgentObj);
});
```
## geolocation.off('fenceStatusChange')8+
off(type: 'fenceStatusChange', request: GeofenceRequest, want: WantAgent) : void;
Unregisters the listener for status change events of the specified geofence.
**Permission required**: ohos.permission.LOCATION
**System capability**: SystemCapability.Location.Location.Geofence
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Event type. The value **fenceStatusChange** indicates a geofence status change.|
| request | GeofenceRequest | Yes| Geofencing request.|
| want | WantAgent | Yes| **WantAgent** used to return geofence (entrance or exit) events.|
**Example**
```js
import geolocation from '@ohos.geolocation';
import wantAgent from '@ohos.wantAgent';
let wantAgentInfo = {
wants: [
{
bundleName: "com.example.myapplication",
abilityName: "com.example.myapplication.MainAbility"
action: "action1",
}
],
operationType: wantAgent.OperationType.START_ABILITY,
requestCode: 0,
wantAgentFlags: [wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
};
wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj) => {
var requestInfo = {'priority': 0x201, 'scenario': 0x301, "geofence": {"latitude": 121, "longitude": 26, "radius": 100, "expiration": 10000}};
geolocation.on('fenceStatusChange', requestInfo, wantAgentObj);
geolocation.off('fenceStatusChange', requestInfo, wantAgentObj);
});
```
## geolocation.on('countryCodeChange')9+
on(type: 'countryCodeChange', callback: Callback<CountryCode>) : void;
Subscribe to country code information reporting events.
**System capability**: SystemCapability.Location.Location.Core
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Event type. The value is "countrycodechange", which means subscribing to the submission of country code information. |
| callback | Callback<CountryCode> | Yes | Callback is used to receive the country code information report. |
**Example**
```js
var callback = (code) => {
console.log('countryCodeChange: ' + JSON.stringify(code));
}
geolocation.on('countryCodeChange', callback);
```
## geolocation.off('countryCodeChange')9+
off(type: 'countryCodeChange', callback?: Callback<CountryCode>) : void;
Unsubscribe from the country code to report events.
**System capability**: SystemCapability.Location.Location.Core
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Event type. The value is "countrycodechange", which means unsubscribing to the submission of country code information. |
| callback | Callback<CountryCode> | Yes | Callback is used to receive the country code information report. |
**Example**
```js
var callback = (code) => {
console.log('countryCodeChange: ' + JSON.stringify(code));
}
geolocation.on('countryCodeChange', callback);
geolocation.off('countryCodeChange', callback);
```
## geolocation.getCurrentLocation
getCurrentLocation(request: CurrentLocationRequest, callback: AsyncCallback<Location>) : void
Obtains the current location. This API uses an asynchronous callback to return the result.
**Permission required**: ohos.permission.LOCATION
**System capability**: SystemCapability.Location.Location.Core
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| request | [CurrentLocationRequest](#currentlocationrequest) | No| Location request.|
| callback | AsyncCallback<[Location](#location)> | Yes| Callback used to return the current location.|
**Example**
```js
var requestInfo = {'priority': 0x203, 'scenario': 0x300,'maxAccuracy': 0};
var locationChange = (err, location) => {
if (err) {
console.log('locationChanger: err=' + JSON.stringify(err));
}
if (location) {
console.log('locationChanger: location=' + JSON.stringify(location));
}
};
geolocation.getCurrentLocation(requestInfo, locationChange);
geolocation.getCurrentLocation(locationChange);
```
## geolocation.getCurrentLocation
getCurrentLocation(request?: CurrentLocationRequest) : Promise<Location>
Obtains the current location. This API uses a promise to return the result.
**Permission required**: ohos.permission.LOCATION
**System capability**: SystemCapability.Location.Location.Core
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| request | [CurrentLocationRequest](#currentlocationrequest) | No| Location request.|
**Return value**
| Name| Description|
| -------- | -------- |
| Promise<[Location](#location)> | Promise used to return the current location.|
**Example**
```js
var requestInfo = {'priority': 0x203, 'scenario': 0x300,'maxAccuracy': 0};
geolocation.getCurrentLocation(requestInfo).then((result) => {
console.log('current location: ' + JSON.stringify(result));
});
```
## geolocation.getLastLocation
getLastLocation(callback: AsyncCallback<Location>) : void
Obtains the previous location. This API uses an asynchronous callback to return the result.
**Permission required**: ohos.permission.LOCATION
**System capability**: SystemCapability.Location.Location.Core
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback<[Location](#location)> | Yes| Callback used to return the previous location.|
**Example**
```js
geolocation.getLastLocation((err, data) => {
if (err) {
console.log('getLastLocation: err=' + JSON.stringify(err));
}
if (data) {
console.log('getLastLocation: data=' + JSON.stringify(data));
}
});
```
## geolocation.getLastLocation
getLastLocation() : Promise<Location>
Obtains the previous location. This API uses a promise to return the result.
**Permission required**: ohos.permission.LOCATION
**System capability**: SystemCapability.Location.Location.Core
**Return value**
| Name| Description|
| -------- | -------- |
| Promise<[Location](#location)> | Promise used to return the previous location.|
**Example**
```js
geolocation.getLastLocation().then((result) => {
console.log('getLastLocation: result: ' + JSON.stringify(result));
});
```
## geolocation.isLocationEnabled
isLocationEnabled(callback: AsyncCallback<boolean>) : void
Checks whether the location service is enabled. This API uses an asynchronous callback to return the result.
**Permission required**: ohos.permission.LOCATION
**System capability**: SystemCapability.Location.Location.Core
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback<boolean> | Yes| Callback used to return the location service status.|
**Example**
```js
geolocation.isLocationEnabled((err, data) => {
if (err) {
console.log('isLocationEnabled: err=' + JSON.stringify(err));
}
if (data) {
console.log('isLocationEnabled: data=' + JSON.stringify(data));
}
});
```
## geolocation.isLocationEnabled
isLocationEnabled() : Promise<boolean>
Checks whether the location service is enabled. This API uses a promise to return the result.
**Permission required**: ohos.permission.LOCATION
**System capability**: SystemCapability.Location.Location.Core
**Return value**
| Name| Description|
| -------- | -------- |
| Promise<boolean> | Promise used to return the location service status.|
**Example**
```js
geolocation.isLocationEnabled().then((result) => {
console.log('promise, isLocationEnabled: ' + result);
});
```
## geolocation.requestEnableLocation
requestEnableLocation(callback: AsyncCallback<boolean>) : void
Requests to enable the location service. This API uses an asynchronous callback to return the result.
**Permission required**: ohos.permission.LOCATION
**System capability**: SystemCapability.Location.Location.Core
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback<boolean> | Yes| Callback used to return the location service status.|
**Example**
```js
geolocation.requestEnableLocation((err, data) => {
if (err) {
console.log('requestEnableLocation: err=' + JSON.stringify(err));
}
if (data) {
console.log('requestEnableLocation: data=' + JSON.stringify(data));
}
});
```
## geolocation.requestEnableLocation
requestEnableLocation() : Promise<boolean>
Requests to enable the location service. This API uses a promise to return the result.
**Permission required**: ohos.permission.LOCATION
**System capability**: SystemCapability.Location.Location.Core
**Return value**
| Name| Description|
| -------- | -------- |
| Promise<boolean> | Promise used to return the location service status.|
**Example**
```js
geolocation.requestEnableLocation().then((result) => {
console.log('promise, requestEnableLocation: ' + JSON.stringify(result));
});
```
## geolocation.enableLocation
enableLocation(callback: AsyncCallback<boolean>) : void;
Enables the location service. This API uses an asynchronous callback to return the result.
**System API**: This is a system API and cannot be called by third-party applications.
**Permission required**: ohos.permission.MANAGE_SECURE_SETTINGS
**System capability**: SystemCapability.Location.Location.Core
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback<boolean> | Yes| Callback used to return the location service status.|
**Example**
```js
geolocation.enableLocation((err, data) => {
if (err) {
console.log('enableLocation: err=' + JSON.stringify(err));
}
if (data) {
console.log('enableLocation: data=' + JSON.stringify(data));
}
});
```
## geolocation.enableLocation
enableLocation() : Promise<boolean>
Enables the location service. This API uses a promise to return the result.
**System API**: This is a system API and cannot be called by third-party applications.
**Permission required**: ohos.permission.MANAGE_SECURE_SETTINGS
**System capability**: SystemCapability.Location.Location.Core
**Return value**
| Name| Description|
| -------- | -------- |
| Promise<boolean> | Promise used to return the location service status.|
**Example**
```js
geolocation.enableLocation().then((result) => {
console.log('promise, enableLocation: ' + JSON.stringify(result));
});
```
## geolocation.disableLocation
disableLocation(callback: AsyncCallback<boolean>) : void;
Disables the location service. This API uses an asynchronous callback to return the result.
**System API**: This is a system API and cannot be called by third-party applications.
**Permission required**: ohos.permission.MANAGE_SECURE_SETTINGS
**System capability**: SystemCapability.Location.Location.Core
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback<boolean> | Yes| Callback used to return the location service status.|
**Example**
```js
geolocation.disableLocation((err, data) => {
if (err) {
console.log('disableLocation: err=' + JSON.stringify(err));
}
if (data) {
console.log('disableLocation: data=' + JSON.stringify(data));
}
});
```
## geolocation.disableLocation
disableLocation() : Promise<boolean>
Disables the location service. This API uses a promise to return the result.
**System API**: This is a system API and cannot be called by third-party applications.
**Permission required**: ohos.permission.MANAGE_SECURE_SETTINGS
**System capability**: SystemCapability.Location.Location.Core
**Return value**
| Name| Description|
| -------- | -------- |
| Promise<boolean> | Promise used to return the location service status.|
**Example**
```js
geolocation.disableLocation().then((result) => {
console.log('promise, disableLocation: ' + JSON.stringify(result));
});
```
## geolocation.isGeoServiceAvailable
isGeoServiceAvailable(callback: AsyncCallback<boolean>) : void
Checks whether the (reverse) geocoding service is available. This API uses an asynchronous callback to return the result.
**Permission required**: ohos.permission.LOCATION
**System capability**: SystemCapability.Location.Location.Geocoder
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback<boolean> | Yes| Callback used to return the (reverse) geocoding service status.|
**Example**
```js
geolocation.isGeoServiceAvailable((err, data) => {
if (err) {
console.log('isGeoServiceAvailable: err=' + JSON.stringify(err));
}
if (data) {
console.log('isGeoServiceAvailable: data=' + JSON.stringify(data));
}
});
```
## geolocation.isGeoServiceAvailable
isGeoServiceAvailable() : Promise<boolean>
Checks whether the (reverse) geocoding service is available. This API uses a promise to return the result.
**Permission required**: ohos.permission.LOCATION
**System capability**: SystemCapability.Location.Location.Geocoder
**Return value**
| Name| Description|
| -------- | -------- |
| Promise<boolean> | Promise used to return the (reverse) geocoding service status.|
**Example**
```js
geolocation.isGeoServiceAvailable().then((result) => {
console.log('promise, isGeoServiceAvailable: ' + JSON.stringify(result));
});
```
## geolocation.getAddressesFromLocation
getAddressesFromLocation(request: ReverseGeoCodeRequest, callback: AsyncCallback<Array<GeoAddress>>) : void
Converts coordinates into geographic description through reverse geocoding. This API uses an asynchronous callback to return the result.
**Permission required**: ohos.permission.LOCATION
**System capability**: SystemCapability.Location.Location.Geocoder
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| request | [ReverseGeoCodeRequest](#reversegeocoderequest) | Yes| Reverse geocoding request.|
| callback | AsyncCallback<Array<[GeoAddress](#geoaddress)>> | Yes| Callback used to return the reverse geocoding result.|
**Example**
```js
var reverseGeocodeRequest = {"latitude": 31.12, "longitude": 121.11, "maxItems": 1};
geolocation.getAddressesFromLocation(reverseGeocodeRequest, (err, data) => {
if (err) {
console.log('getAddressesFromLocation: err=' + JSON.stringify(err));
}
if (data) {
console.log('getAddressesFromLocation: data=' + JSON.stringify(data));
}
});
```
## geolocation.getAddressesFromLocation
getAddressesFromLocation(request: ReverseGeoCodeRequest) : Promise<Array<GeoAddress>>;
Converts coordinates into geographic description through reverse geocoding. This API uses a promise to return the result.
**Permission required**: ohos.permission.LOCATION
**System capability**: SystemCapability.Location.Location.Geocoder
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| request | [ReverseGeoCodeRequest](#reversegeocoderequest) | Yes| Reverse geocoding request.|
**Return value**
| Name| Description|
| -------- | -------- |
| Promise<Array<[GeoAddress](#geoaddress)>> | Promise used to return the reverse geocoding result.|
**Example**
```js
var reverseGeocodeRequest = {"latitude": 31.12, "longitude": 121.11, "maxItems": 1};
geolocation.getAddressesFromLocation(reverseGeocodeRequest).then((data) => {
console.log('getAddressesFromLocation: ' + JSON.stringify(data));
});
```
## geolocation.getAddressesFromLocationName
getAddressesFromLocationName(request: GeoCodeRequest, callback: AsyncCallback<Array<GeoAddress>>) : void
Converts geographic description into coordinates through geocoding. This API uses an asynchronous callback to return the result.
**Permission required**: ohos.permission.LOCATION
**System capability**: SystemCapability.Location.Location.Geocoder
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| request | [GeoCodeRequest](#geocoderequest) | Yes| Geocoding request.|
| callback | AsyncCallback<Array<[GeoAddress](#geoaddress)>> | Yes| Callback used to return the geocoding result.|
**Example**
```js
var geocodeRequest = {"description": "No. xx, xx Road, Pudong District, Shanghai", "maxItems": 1};
geolocation.getAddressesFromLocationName(geocodeRequest, (err, data) => {
if (err) {
console.log('getAddressesFromLocationName: err=' + JSON.stringify(err));
}
if (data) {
console.log('getAddressesFromLocationName: data=' + JSON.stringify(data));
}
});
```
## geolocation.getAddressesFromLocationName
getAddressesFromLocationName(request: GeoCodeRequest) : Promise<Array<GeoAddress>>
Converts geographic description into coordinates through geocoding. This API uses a promise to return the result.
**Permission required**: ohos.permission.LOCATION
**System capability**: SystemCapability.Location.Location.Geocoder
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| request | [GeoCodeRequest](#geocoderequest) | Yes| Geocoding request.|
**Return value**
| Name| Description|
| -------- | -------- |
| Promise<Array<[GeoAddress](#geoaddress)>> | Callback used to return the geocoding result.|
**Example**
```js
var geocodeRequest = {"description": "No. xx, xx Road, Pudong District, Shanghai", "maxItems": 1};
geolocation.getAddressesFromLocationName(geocodeRequest).then((result) => {
console.log('getAddressesFromLocationName: ' + JSON.stringify(result));
});
```
## geolocation.getCachedGnssLocationsSize8+
getCachedGnssLocationsSize(callback: AsyncCallback<number>) : void;
Obtains the number of cached GNSS locations.
**Permission required**: ohos.permission.LOCATION
**System capability**: SystemCapability.Location.Location.Gnss
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback<number> | Yes| Callback used to return the number of cached GNSS locations. |
**Example**
```js
geolocation.getCachedGnssLocationsSize((err, size) => {
if (err) {
console.log('getCachedGnssLocationsSize: err=' + JSON.stringify(err));
}
if (size) {
console.log('getCachedGnssLocationsSize: size=' + JSON.stringify(size));
}
});
```
## geolocation.getCachedGnssLocationsSize8+
getCachedGnssLocationsSize() : Promise<number>;
Obtains the number of cached GNSS locations.
**Permission required**: ohos.permission.LOCATION
**System capability**: SystemCapability.Location.Location.Gnss
**Return value**
| Name| Description|
| -------- | -------- |
| Promise<number> | Promise used to return the number of cached GNSS locations.|
**Example**
```js
geolocation.getCachedGnssLocationsSize().then((result) => {
console.log('promise, getCachedGnssLocationsSize: ' + JSON.stringify(result));
});
```
## geolocation.flushCachedGnssLocations8+
flushCachedGnssLocations(callback: AsyncCallback<boolean>) : void;
Obtains all cached GNSS locations and clears the GNSS cache queue.
**Permission required**: ohos.permission.LOCATION
**System capability**: SystemCapability.Location.Location.Gnss
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback<boolean> | Yes| Callback used to return the operation result.|
**Example**
```js
geolocation.flushCachedGnssLocations((err, result) => {
if (err) {
console.log('flushCachedGnssLocations: err=' + JSON.stringify(err));
}
if (result) {
console.log('flushCachedGnssLocations: result=' + JSON.stringify(result));
}
});
```
## geolocation.flushCachedGnssLocations8+
flushCachedGnssLocations() : Promise<boolean>;
Obtains all cached GNSS locations and clears the GNSS cache queue.
**Permission required**: ohos.permission.LOCATION
**System capability**: SystemCapability.Location.Location.Gnss
**Return value**
| Name| Description|
| -------- | -------- |
| Promise<boolean> | Promise used to return the operation result.|
**Example**
```js
geolocation.flushCachedGnssLocations().then((result) => {
console.log('promise, flushCachedGnssLocations: ' + JSON.stringify(result));
});
```
## geolocation.sendCommand8+
sendCommand(command: LocationCommand, callback: AsyncCallback<boolean>) : void;
Sends an extended command to the location subsystem. This API can only be called by system applications.
**Permission required**: ohos.permission.LOCATION
**System capability**: SystemCapability.Location.Location.Core
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| command | LocationCommand | Yes| Extended command (string) to be sent.|
| callback | AsyncCallback<boolean> | Yes| Callback used to return the operation result.|
**Example**
```js
var requestInfo = {'scenario': 0x301, 'command': "command_1"};
geolocation.sendCommand(requestInfo, (err, result) => {
if (err) {
console.log('sendCommand: err=' + JSON.stringify(err));
}
if (result) {
console.log('sendCommand: result=' + JSON.stringify(result));
}
});
```
## geolocation.sendCommand8+
sendCommand(command: LocationCommand) : Promise<boolean>;
Sends an extended command to the location subsystem. This API can only be called by system applications.
**Permission required**: ohos.permission.LOCATION
**System capability**: SystemCapability.Location.Location.Core
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| command | LocationCommand | Yes| Extended command (string) to be sent.|
**Return value**
| Name| Description|
| -------- | -------- |
| Promise<boolean> | Callback used to return the operation result.|
**Example**
```js
var requestInfo = {'scenario': 0x301, 'command': "command_1"};
geolocation.sendCommand(requestInfo).then((result) => {
console.log('promise, sendCommand: ' + JSON.stringify(result));
});
```
## geolocation.isLocationPrivacyConfirmed8+
isLocationPrivacyConfirmed(type : LocationPrivacyType, callback: AsyncCallback<boolean>) : void;
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 and cannot be called by third-party applications.
**Permission required**: ohos.permission.LOCATION
**System capability**: SystemCapability.Location.Location.Core
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| type | LocationPrivacyType | Yes| Privacy statement type, for example, privacy statement displayed in the startup wizard or privacy statement displayed when enabling the location service.|
| callback | AsyncCallback<boolean> | Yes| Callback used to return the result, which indicates whether the user agrees with the privacy statement.|
**Example**
```js
geolocation.isLocationPrivacyConfirmed(1, (err, result) => {
if (err) {
console.log('isLocationPrivacyConfirmed: err=' + JSON.stringify(err));
}
if (result) {
console.log('isLocationPrivacyConfirmed: result=' + JSON.stringify(result));
}
});
```
## geolocation.isLocationPrivacyConfirmed8+
isLocationPrivacyConfirmed(type : LocationPrivacyType,) : Promise<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 and cannot be called by third-party applications.
**Permission required**: ohos.permission.LOCATION
**System capability**: SystemCapability.Location.Location.Core
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| type | LocationPrivacyType | Yes| Privacy statement type, for example, privacy statement displayed in the startup wizard or privacy statement displayed when enabling the location service.|
**Return value**
| Name| Description|
| -------- | -------- |
| Promise<boolean> | Callback used to return the result, which indicates whether the user agrees with the privacy statement.|
**Example**
```js
geolocation.isLocationPrivacyConfirmed(1).then((result) => {
console.log('promise, isLocationPrivacyConfirmed: ' + JSON.stringify(result));
});
```
## geolocation.setLocationPrivacyConfirmStatus8+
setLocationPrivacyConfirmStatus(type : LocationPrivacyType, isConfirmed: boolean, callback: AsyncCallback<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 and cannot be called by third-party applications.
**Permission required**: ohos.permission.LOCATION
**System capability**: SystemCapability.Location.Location.Core
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| type | LocationPrivacyType | Yes| Privacy statement type, for example, privacy statement displayed in the startup wizard or privacy statement displayed when enabling the location service.|
| isConfirmed | boolean | Yes| Callback used to return the result, which indicates whether the user agrees with the privacy statement.|
| callback | AsyncCallback<boolean> | Yes| Callback used to return the operation result.|
**Example**
```js
geolocation.setLocationPrivacyConfirmStatus(1, true, (err, result) => {
if (err) {
console.log('setLocationPrivacyConfirmStatus: err=' + JSON.stringify(err));
}
if (result) {
console.log('setLocationPrivacyConfirmStatus: result=' + JSON.stringify(result));
}
});
```
## geolocation.setLocationPrivacyConfirmStatus8+
setLocationPrivacyConfirmStatus(type : LocationPrivacyType, isConfirmed : boolean) : Promise<boolean>;
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 and cannot be called by third-party applications.
**Permission required**: ohos.permission.LOCATION
**System capability**: SystemCapability.Location.Location.Core
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| type | LocationPrivacyType | Yes| Privacy statement type, for example, privacy statement displayed in the startup wizard or privacy statement displayed when enabling the location service.|
| isConfirmed | boolean | Yes| Callback used to return the result, which indicates whether the user agrees with the privacy statement.|
**Return value**
| Name| Description|
| -------- | -------- |
| Promise<boolean> | Callback used to return the operation result.|
**Example**
```js
geolocation.setLocationPrivacyConfirmStatus(1, true).then((result) => {
console.log('promise, setLocationPrivacyConfirmStatus: ' + JSON.stringify(result));
});
```
## geolocation.getCountryCode9+
getCountryCode(callback: AsyncCallback<CountryCode>) : void;
Query the current country code.
**System capability**: SystemCapability.Location.Location.Core
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback<CountryCode> | Yes | Callback is used to receive the country code. |
**Example**:
```js
geolocation.getCountryCode((err, result) => {
if (err) {
console.log('getCountryCode: err=' + JSON.stringify(err));
}
if (result) {
console.log('getCountryCode: result=' + JSON.stringify(result));
}
});
```
## geolocation.getCountryCode9+
getCountryCode() : Promise<CountryCode>;
Query the current country code.
**System capability**: SystemCapability.Location.Location.Core
**Parameters**
None
**Return value**
| Name| Description|
| -------- | -------- |
| Promise<CountryCode> | return country code. |
**Example**:
```js
geolocation.getCountryCode()
.then((result) => {
console.log('promise, getCountryCode: result=' + JSON.stringify(result));
})
.catch((error) => {
console.log('promise, getCountryCode: error=' + JSON.stringify(error));
});
```
## geolocation.enableLocationMock9+
enableLocationMock(scenario?: LocationRequestScenario, callback: AsyncCallback<void>) : void;
Enable the position simulation function of a scene, and only one scene can be enabled at the same time.
**System capability**: SystemCapability.Location.Location.Core
**System API**: This is a system API and cannot be called by third-party applications.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| scenario | LocationRequestScenario | No | Indicates under what scenario the position simulation function is enabled. |
| callback | AsyncCallback<void> | Yes | It is used to receive the execution result. If the execution is successful, it will return nullptr. Otherwise, it will return an error message. |
**Example**:
```js
var request = {"scenario": 0x0301};
geolocation.enableLocationMock(request, (err, result) => {
if (err) {
console.log('enableLocationMock: err=' + JSON.stringify(err));
}
if (result) {
console.log('enableLocationMock: result=' + JSON.stringify(result));
}
});
```
## geolocation.enableLocationMock9+
enableLocationMock(scenario?: LocationRequestScenario) : Promise<void>;
Enable the position simulation function of a scene, and only one scene can be enabled at the same time.
**System capability**: SystemCapability.Location.Location.Core
**System API**: This is a system API and cannot be called by third-party applications.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| scenario | LocationRequestScenario | No | Indicates which scene's position simulation function is enabled. If this parameter is not carried, it means that the position simulation function of all scenes is enabled. |
**Return value**
| Name| Description|
| -------- | -------- |
| Promise<void> | It is used to receive the execution result. If the execution is successful, it will return nullptr. Otherwise, it will return an error message. |
**Example**:
```js
var request = {"scenario": 0x0301};
geolocation.enableLocationMock(request)
.then((result) => {
if (result) {
console.log('promise, enableLocationMock: result=' + JSON.stringify(result));
}
})
.catch((error) => {
if (error) {
console.log('promise, enableLocationMock: error=' + JSON.stringify(error));
}
});
```
## geolocation.disableLocationMock9+
disableLocationMock(scenario?: LocationRequestScenario, callback: AsyncCallback<void>) : void;
To disable the position simulation function.
**System capability**: SystemCapability.Location.Location.Core
**System API**: This is a system API and cannot be called by third-party applications.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| scenario | LocationRequestScenario | No | Indicates to disable the position simulation function of a scene. If this parameter is not carried, it means to disable the position simulation function of all scenes. |
| callback | AsyncCallback<void> | Yes | It is used to receive the execution result. If the execution is successful, it will return nullptr. Otherwise, it will return an error message. |
**Example**:
```js
var request = {"scenario": 0x0301};
geolocation.disableLocationMock(request, (err, result) => {
if (err) {
console.log('disableLocationMock: err=' + JSON.stringify(err));
}
if (result) {
console.log('disableLocationMock: result=' + JSON.stringify(result));
}
});
```
## geolocation.disableLocationMock9+
disableLocationMock(scenario?: LocationRequestScenario) : Promise<void>;
To disable the position simulation function.
**System capability**: SystemCapability.Location.Location.Core
**System API**: This is a system API and cannot be called by third-party applications.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| scenario | LocationRequestScenario | No | Indicates to disable the position simulation function of a scene. If this parameter is not carried, it means to disable the position simulation function of all scenes. |
**Return value**
| Name| Description|
| -------- | -------- |
| Promise<void> | It is used to receive the execution result. If the execution is successful, it will return nullptr, otherwise it will return an error message |
**Example**:
```js
var request = {"scenario": 0x0301};
geolocation.disableLocationMock(request)
.then((result) => {
if (result) {
console.log('promise, disableLocationMock: result=' + JSON.stringify(result));
}
})
.catch((error) => {
if (error) {
console.log('promise, disableLocationMock: error=' + JSON.stringify(error));
}
});
```
## geolocation.setMockedLocations9+
setMockedLocations(config: LocationMockConfig, callback: AsyncCallback<void>) : void;
Set the simulated location information, and then report the simulated location at the time interval carried in the interface.
**System capability**: SystemCapability.Location.Location.Core
**System API**: This is a system API and cannot be called by third-party applications.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| config | LocationMockConfig | Yes | Indicates the configuration parameters of location simulation, including the time interval of simulation location reporting and the array of simulation locations. |
| callback | AsyncCallback<void> | Yes | It is used to receive the execution result. If the execution is successful, it will return nullptr. Otherwise, it will return an error message. |
**Example**:
```js
var locations = [
{"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}
];
var config = {"timeInterval": 5, "locations": locations};
geolocation.setMockedLocations(config, (err, data) => {
if (err) {
console.log('setMockedLocations: err=' + JSON.stringify(err));
}
if (data) {
console.log('setMockedLocations: data=' + JSON.stringify(data));
}
});
```
## geolocation.setMockedLocations9+
setMockedLocations(config: LocationMockConfig) : Promise<void>;
Set the simulated location information, and then report the simulated location at the time interval carried in the interface.
**System capability**: SystemCapability.Location.Location.Core
**System API**: This is a system API and cannot be called by third-party applications.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| config | LocationMockConfig | Yes | Indicates the configuration parameters of location simulation, including the time interval of simulation location reporting and the array of simulation locations. |
**Return value**
| Name| Description|
| -------- | -------- |
| Promise<void> | It is used to receive the execution result. If the execution is successful, it will return nullptr. Otherwise, it will return an error message. |
**Example**:
```js
var locations = [
{"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}
];
var config = {"timeInterval": 5, "locations":locations};
geolocation.setMockedLocations(config)
.then((result) => {
if (result) {
console.log('promise, setMockedLocations: result=' + JSON.stringify(result));
}
})
.catch((error) => {
if (error) {
console.log('promise, setMockedLocations: error=' + JSON.stringify(error));
}
});
```
## geolocation.enableReverseGeocodingMock9+
enableReverseGeocodingMock(callback: AsyncCallback<void>) : void;
Enable reverse geocoding simulation function.
**System capability**: SystemCapability.Location.Location.Core
**System API**: This is a system API and cannot be called by third-party applications.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback<void> | Yes | It is used to receive the execution result. If the execution is successful, it will return nullptr. Otherwise, it will return an error message. |
**Example**:
```js
geolocation.enableReverseGeocodingMock((err, data) => {
if (err) {
console.log('enableReverseGeocodingMock: err=' + JSON.stringify(err));
}
if (data) {
console.log('enableReverseGeocodingMock: data=' + JSON.stringify(data));
}
});
```
## geolocation.enableReverseGeocodingMock9+
enableReverseGeocodingMock() : Promise<void>;
Enable reverse geocoding simulation function.
**System capability**: SystemCapability.Location.Location.Core
**System API**: This is a system API and cannot be called by third-party applications.
**Parameters**:
None
**Return value**
| Name| Description|
| -------- | -------- |
| Promise<void> | It is used to receive the execution result. If the execution is successful, it will return nullptr. Otherwise, it will return an error message. |
**Example**:
```js
geolocation.enableReverseGeocodingMock()
.then((result) => {
if (result) {
console.log('promise, enableReverseGeocodingMock: result=' + JSON.stringify(result));
}
})
.catch((error) => {
if (error) {
console.log('promise, enableReverseGeocodingMock: error=' + JSON.stringify(error));
}
});
```
## geolocation.disableReverseGeocodingMock9+
disableReverseGeocodingMock(callback: AsyncCallback<void>) : void;
Disable reverse geocoding simulation function.
**System capability**: SystemCapability.Location.Location.Core
**System API**: This is a system API and cannot be called by third-party applications.
**Parameters**:
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback<void> | Yes | It is used to receive the execution result. If the execution is successful, it will return nullptr. Otherwise, it will return an error message |
**Example**:
```js
geolocation.disableReverseGeocodingMock((err, result) => {
if (err) {
console.log('disableReverseGeocodingMock: err=' + JSON.stringify(err));
}
if (result) {
console.log('disableReverseGeocodingMock: result=' + JSON.stringify(result));
}
});
```
## geolocation.disableReverseGeocodingMock9+
disableReverseGeocodingMock() : Promise<void>;
Disable reverse geocoding simulation function.
**System capability**: SystemCapability.Location.Location.Core
**System API**: This is a system API and cannot be called by third-party applications.
**Parameters**:
None
**Return value**
| Name| Description|
| -------- | -------- |
| Promise<void> | It is used to receive the execution result. If the execution is successful, it will return nullptr. Otherwise, it will return an error message. |
**Example**:
```js
geolocation.disableReverseGeocodingMock()
.then((result) => {
if (result) {
console.log('promise, disableReverseGeocodingMock: result=' + JSON.stringify(result));
}
})
.catch((error) => {
if (error) {
console.log('promise, disableReverseGeocodingMock: error=' + JSON.stringify(error));
}
});
```
## geolocation.setReverseGeocodingMockInfo9+
setReverseGeocodingMockInfo(mockInfos: Array<ReverseGeocodingMockInfo>, callback: AsyncCallback<void>) : void;
Set the configuration information of the reverse geocoding simulation function, including the corresponding relationship between location and place name. If the location information is in the configuration information during the subsequent reverse geocoding query, the corresponding place name will be returned.
**System capability**: SystemCapability.Location.Location.Core
**System API**: This is a system API and cannot be called by third-party applications.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| mockInfos | Array<ReverseGeocodingMockInfo> | Yes | An array of configuration parameters indicating the inverse geocoding simulation function. The configuration parameters of the inverse geocoding simulation function include a location and a place name. |
| callback | AsyncCallback<void> | Yes | It is used to receive the execution result. If the execution is successful, it will return nullptr. Otherwise, it will return an error message. |
**Example**:
```js
var mockInfos = [
{"location": {"locale": "zh", "latitude": 30.12, "longitude": 120.11, "maxItems": 1}, "geoAddress": {"locale": "zh", "latitude": 30.12, "longitude": 120.11, "maxItems": 1, "isFromMock": true}},
{"location": {"locale": "zh", "latitude": 31.12, "longitude": 121.11, "maxItems": 1}, "geoAddress": {"locale": "zh", "latitude": 31.12, "longitude": 121.11, "maxItems": 1, "isFromMock": true}},
{"location": {"locale": "zh", "latitude": 32.12, "longitude": 122.11, "maxItems": 1}, "geoAddress": {"locale": "zh", "latitude": 32.12, "longitude": 122.11, "maxItems": 1, "isFromMock": true}},
{"location": {"locale": "zh", "latitude": 33.12, "longitude": 123.11, "maxItems": 1}, "geoAddress": {"locale": "zh", "latitude": 33.12, "longitude": 123.11, "maxItems": 1, "isFromMock": true}},
{"location": {"locale": "zh", "latitude": 34.12, "longitude": 124.11, "maxItems": 1}, "geoAddress": {"locale": "zh", "latitude": 34.12, "longitude": 124.11, "maxItems": 1, "isFromMock": true}},
];
geolocation.setReverseGeocodingMockInfo(mockInfos, (err, data) => {
if (err) {
console.log('promise, setReverseGeocodingMockInfo, err:' + JSON.stringify(err));
}
if (data) {
console.log('promise, setReverseGeocodingMockInfo, data:' + JSON.stringify(data));
}
});
```
## geolocation.setReverseGeocodingMockInfo9+
setReverseGeocodingMockInfo(mockInfos: Array<ReverseGeocodingMockInfo>) : Promise<void>;
Set the configuration information of the reverse geocoding simulation function, including the corresponding relationship between location and place name. If the location information is in the configuration information during the subsequent reverse geocoding query, the corresponding place name will be returned.
**System capability**: SystemCapability.Location.Location.Core
**System API**: This is a system API and cannot be called by third-party applications.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| mockInfos | Array<ReverseGeocodingMockInfo> | Yes | An array of configuration parameters indicating the inverse geocoding simulation function. The configuration parameters of the inverse geocoding simulation function include a location and a place name. |
**Return value**
| Name| Description|
| -------- | -------- |
| Promise<void> | It is used to receive the execution result. If the execution is successful, it will return nullptr. Otherwise, it will return an error message. |
**Example**:
```js
var mockInfos = [
{"location": {"locale": "zh", "latitude": 30.12, "longitude": 120.11, "maxItems": 1}, "geoAddress": {"locale": "zh", "latitude": 30.12, "longitude": 120.11, "maxItems": 1, "isFromMock": true}},
{"location": {"locale": "zh", "latitude": 31.12, "longitude": 121.11, "maxItems": 1}, "geoAddress": {"locale": "zh", "latitude": 31.12, "longitude": 121.11, "maxItems": 1, "isFromMock": true}},
{"location": {"locale": "zh", "latitude": 32.12, "longitude": 122.11, "maxItems": 1}, "geoAddress": {"locale": "zh", "latitude": 32.12, "longitude": 122.11, "maxItems": 1, "isFromMock": true}},
{"location": {"locale": "zh", "latitude": 33.12, "longitude": 123.11, "maxItems": 1}, "geoAddress": {"locale": "zh", "latitude": 33.12, "longitude": 123.11, "maxItems": 1, "isFromMock": true}},
{"location": {"locale": "zh", "latitude": 34.12, "longitude": 124.11, "maxItems": 1}, "geoAddress": {"locale": "zh", "latitude": 34.12, "longitude": 124.11, "maxItems": 1, "isFromMock": true}},
];
geolocation.setReverseGeocodingMockInfo(mockInfos)
.then((result) => {
if (result) {
console.log('promise, setReverseGeocodingMockInfo: result=' + JSON.stringify(result));
}
})
.catch((error) => {
if (error) {
console.log('promise, setReverseGeocodingMockInfo: error=' + JSON.stringify(error));
}
});
```
## LocationRequestPriority
Sets the priority of the location request.
**Permission required**: ohos.permission.LOCATION
**System capability**: SystemCapability.Location.Location.Core
| Name| Default Value| Description|
| -------- | -------- | -------- |
| UNSET | 0x200 | Priority unspecified.|
| ACCURACY | 0x201 | Location accuracy.|
| LOW_POWER | 0x202 | Power efficiency.|
| FIRST_FIX | 0x203 | Fast location. Use this option if you want to obtain a location as fast as possible.|
## LocationRequestScenario
Sets the scenario of the location request.
**Permission required**: ohos.permission.LOCATION
**System capability**: SystemCapability.Location.Location.Core
| Name| Default Value| Description|
| -------- | -------- | -------- |
| UNSET | 0x300 | Scenario unspecified.|
| NAVIGATION | 0x301 | Navigation.|
| TRAJECTORY_TRACKING | 0x302 | Trajectory tracking.|
| CAR_HAILING | 0x303 | Ride hailing.|
| DAILY_LIFE_SERVICE | 0x304 | Daily life services.|
| NO_POWER | 0x305 | Power efficiency. Your application does not proactively start the location service. When responding to another application requesting the same location service, the system marks a copy of the location result to your application. In this way, your application will not consume extra power for obtaining the user location.|
## GeoLocationErrorCode
Enumerates error codes of the location service.
**Permission required**: ohos.permission.LOCATION
**System capability**: SystemCapability.Location.Location.Core
| Name| Default Value| Description|
| -------- | -------- | -------- |
| NOT_SUPPORTED9+ | 100 | Indicates that the interface function is not supported. |
| INPUT_PARAMS_ERROR7+ | 101 | Incorrect input parameters.|
| REVERSE_GEOCODE_ERROR7+ | 102 | Failed to call the reverse geocoding API.|
| GEOCODE_ERROR7+ | 103 | Failed to call the geocoding API.|
| LOCATOR_ERROR7+ | 104 | Failed to obtain the location.|
| LOCATION_SWITCH_ERROR7+ | 105 | Failed to change the location service switch.|
| LAST_KNOWN_LOCATION_ERROR7+ | 106 | Failed to obtain the previous location.|
| LOCATION_REQUEST_TIMEOUT_ERROR7+ | 107 | Failed to obtain the location within the specified time.|
| QUERY_COUNTRY_CODE_ERROR9+ | 108 | Indicates that the country code query failed. |
## ReverseGeoCodeRequest
Defines a reverse geocoding request.
**Permission required**: ohos.permission.LOCATION
**System capability**: SystemCapability.Location.Location.Geocoder
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| locale | string | No| Language used for the location description. **zh** indicates Chinese, and **en** indicates English.|
| latitude | number | Yes| Latitude information. A positive value indicates north latitude, and a negative value indicates south latitude.|
| longitude | number | Yes| Longitude information. A positive value indicates east longitude , and a negative value indicates west longitude .|
| maxItems | number | No| Maximum number of location records to be returned.|
## GeoCodeRequest
Defines a geocoding request.
**Permission required**: ohos.permission.LOCATION
**System capability**: SystemCapability.Location.Location.Geocoder
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| locale | string | No| Language used for the location description. **zh** indicates Chinese, and **en** indicates English.|
| description | number | Yes| Location description, for example, No. xx, xx Road, Pudong New District, Shanghai.|
| maxItems | number | No| Maximum number of location records to be returned.|
| minLatitude | number | No| Minimum latitude. This parameter is used with minLongitude, maxLatitude, and maxLongitude to specify the latitude and longitude ranges.|
| minLongitude | number | No| Minimum longitude.|
| maxLatitude | number | No| Maximum latitude.|
| maxLongitude | number | No| Maximum longitude.|
## GeoAddress
Defines a geographic location.
**Permission required**: ohos.permission.LOCATION
**System capability**: SystemCapability.Location.Location.Geocoder
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| latitude7+ | number | No| Latitude information. A positive value indicates north latitude, and a negative value indicates south latitude.|
| longitude7+ | number | No| Longitude information. A positive value indicates east longitude , and a negative value indicates west longitude .|
| locale7+ | string | No| Language used for the location description. **zh** indicates Chinese, and **en** indicates English.|
| placeName7+ | string | No| Landmark of the location.|
| countryCode7+ | string | No| Country code.|
| countryName7+ | string | No| Country name.|
| administrativeArea7+ | string | No| Administrative region name.|
| subAdministrativeArea7+ | string | No| Sub-administrative region name.|
| locality7+ | string | No| Locality information. |
| subLocality7+ | string | No| Sub-locality information. |
| roadName7+ | string | No| Road name.|
| subRoadName7+ | string | No| Auxiliary road information.|
| premises7+ | string | No| House information.|
| postalCode7+ | string | No| Postal code.|
| phoneNumber7+ | string | No| Phone number.|
| addressUrl7+ | string | No| Website URL.|
| descriptions7+ | Array<string> | No| Additional description.|
| descriptionsSize7+ | number | No| Total number of additional descriptions.|
| isFromMock9+ | Boolean | No | Indicates whether the geographical name information comes from the reverse geocoding simulation function. |
## LocationRequest
Defines a location request.
**Permission required**: ohos.permission.LOCATION
**System capability**: SystemCapability.Location.Location.Core
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| priority | [LocationRequestPriority](#locationrequestpriority) | No| Priority of the location request.|
| scenario | [LocationRequestScenario](#locationrequestscenario) | Yes| Scenario of the location request.|
| timeInterval | number | No| Time interval at which location information is reported.|
| distanceInterval | number | No| Distance interval at which location information is reported.|
| maxAccuracy | number | No| Location accuracy.|
## CurrentLocationRequest
Defines the current location request.
**Permission required**: ohos.permission.LOCATION
**System capability**: SystemCapability.Location.Location.Core
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| priority | [LocationRequestPriority](#locationrequestpriority) | No| Priority of the location request.|
| scenario | [LocationRequestScenario](#locationrequestscenario) | No| Scenario of the location request.|
| maxAccuracy | number | No| Location accuracy, in meters.|
| timeoutMs | number | No| Timeout duration, in milliseconds. The minimum value is 1000.|
## SatelliteStatusInfo8+
Defines the satellite status information.
**Permission required**: ohos.permission.LOCATION
**System capability**: SystemCapability.Location.Location.Gnss
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| satellitesNumber | number | Yes| Number of satellites.|
| satelliteIds | Array<number> | Yes| Array of satellite IDs.|
| carrierToNoiseDensitys | Array<number> | Yes| Carrier-to-noise density ratio, that is, **cn0**.|
| altitudes | Array<number> | Yes| Altitude information.|
| azimuths | Array<number> | Yes| Azimuth information.|
| carrierFrequencies | Array<number> | Yes| Carrier frequency.|
## CachedGnssLocationsRequest8+
Represents a request for reporting cached GNSS locations.
**Permission required**: ohos.permission.LOCATION
**System capability**: SystemCapability.Location.Location.Gnss
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| reportingPeriodSec | number | Yes| Interval for reporting the cached GNSS locations, in milliseconds.|
| wakeUpCacheQueueFull | boolean | Yes| **true**: reports the cached GNSS locations to the application when the cache queue is full.
**false**: discards the cached GNSS locations when the cache queue is full.|
## Geofence8+
Defines a GNSS geofence. Currently, only circular geofences are supported.
**Permission required**: ohos.permission.LOCATION
**System capability**: SystemCapability.Location.Location.Geofence
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| latitude | number | Yes| Latitude information.|
| longitude | number | Yes| Longitude information.|
| radius | number | Yes| Radius of a circular geofence.|
| expiration | number | Yes| Expiration period of a geofence, in milliseconds.|
## GeofenceRequest8+
Represents a GNSS geofencing request.
**Permission required**: ohos.permission.LOCATION
**System capability**: SystemCapability.Location.Location.Geofence
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| priority | LocationRequestPriority | Yes| Priority of the location information.|
| scenario | LocationRequestScenario | Yes| Location scenario.|
| geofence | Geofence | Yes| Geofence information.|
## LocationPrivacyType8+
Defines the privacy statement type.
**Permission required**: ohos.permission.LOCATION
**System capability**: SystemCapability.Location.Location.Core
| Name| Default Value| Description|
| -------- | -------- | -------- |
| OTHERS | 0 | Other scenarios.|
| STARTUP | 1 | Privacy statement displayed in the startup wizard.|
| CORE_LOCATION | 2 | Privacy statement displayed when enabling the location service.|
## LocationCommand8+
Defines an extended command.
**Permission required**: ohos.permission.LOCATION
**System capability**: SystemCapability.Location.Location.Core
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| scenario | LocationRequestScenario | Yes| Location scenario.|
| command | string | Yes| Extended command, in the string format.|
## Location
Defines a location.
**Permission required**: ohos.permission.LOCATION
**System capability**: SystemCapability.Location.Location.Core
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| latitude7+ | number | Yes| Latitude information. A positive value indicates north latitude, and a negative value indicates south latitude.|
| longitude7+ | number | Yes| Longitude information. A positive value indicates east longitude , and a negative value indicates west longitude .|
| altitude7+ | number | Yes| Location altitude, in meters.|
| accuracy7+ | number | Yes| Location accuracy, in meters.|
| speed7+ | number | Yes| Speed, in m/s.|
| timeStamp7+ | number | Yes| Location timestamp in the UTC format.|
| direction7+ | number | Yes| Direction information.|
| timeSinceBoot7+ | number | Yes| Location timestamp since boot.|
| additions7+ | Array<string> | No| Additional information.|
| additionSize7+ | number | No| Number of additional descriptions.|
| isFromMock9+ | Boolean | No | Indicates whether the location information comes from the location simulation function. |
## ReverseGeocodingMockInfo9+
The configuration information of the reverse geocoding simulation function includes a location information and a place name information.
**System capability**:SystemCapability.Location.Location.Core
**System API**: This is a system API and cannot be called by third-party applications.
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| location | ReverseGeoCodeRequest | Yes | Indicates longitude and latitude information. |
| geoAddress | GeoAddress | Yes | Represents a geographic location. |
## LocationMockConfig9+
The configuration parameters of the location simulation function include the time interval of the simulation position report and the array of simulation locations.
**System capability**: SystemCapability.Location.Location.Core
**System API**: This is a system API and cannot be called by third-party applications.
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| timeInterval | number | Yes | Indicates the time interval of analog location reporting, in seconds. |
| locations | Array<Location> | Yes | Represents an array of mocked locations. |
## CountryCode9+
The country code information structure contains the country code string and the source information of the country code.
**System capability**: SystemCapability.Location.Location.Core
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| country | string | Yes | Represents the country code string. |
| type | CountryCodeType | Yes | Indicates the source of country code information. |
## CountryCodeType9+
Country code source type.
**System capability**: SystemCapability.Location.Location.Core
| Name| Default Value| Description|
| -------- | -------- | -------- |
| COUNTRY_CODE_FROM_LOCALE | 1 | The country code obtained from the language configuration information of the globalization module. |
| COUNTRY_CODE_FROM_SIM | 2 | The country code obtained from the SIM card. |
| COUNTRY_CODE_FROM_LOCATION | 3 | Based on the user's location information, the country code is queried through reverse geocoding. |
| COUNTRY_CODE_FROM_NETWORK | 4 | The country code obtained from the cellular network registration information. |