# 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
import geolocation from '@ohos.geolocation';
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
import geolocation from '@ohos.geolocation';
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
import geolocation from '@ohos.geolocation';
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
import geolocation from '@ohos.geolocation';
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
import geolocation from '@ohos.geolocation';
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
import geolocation from '@ohos.geolocation';
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
import geolocation from '@ohos.geolocation';
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
import geolocation from '@ohos.geolocation';
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
import geolocation from '@ohos.geolocation';
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
import geolocation from '@ohos.geolocation';
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.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
import geolocation from '@ohos.geolocation';
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
import geolocation from '@ohos.geolocation';
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
import geolocation from '@ohos.geolocation';
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
import geolocation from '@ohos.geolocation';
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
import geolocation from '@ohos.geolocation';
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
import geolocation from '@ohos.geolocation';
geolocation.isLocationEnabled().then((result) => {
console.log('promise, isLocationEnabled: ' + JSON.stringify(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
import geolocation from '@ohos.geolocation';
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
import geolocation from '@ohos.geolocation';
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
import geolocation from '@ohos.geolocation';
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
import geolocation from '@ohos.geolocation';
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
import geolocation from '@ohos.geolocation';
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
import geolocation from '@ohos.geolocation';
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
import geolocation from '@ohos.geolocation';
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
import geolocation from '@ohos.geolocation';
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
import geolocation from '@ohos.geolocation';
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
import geolocation from '@ohos.geolocation';
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
import geolocation from '@ohos.geolocation';
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
import geolocation from '@ohos.geolocation';
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
import geolocation from '@ohos.geolocation';
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
import geolocation from '@ohos.geolocation';
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
import geolocation from '@ohos.geolocation';
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
import geolocation from '@ohos.geolocation';
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
import geolocation from '@ohos.geolocation';
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
import geolocation from '@ohos.geolocation';
var requestInfo = {'scenario': 0x301, 'command': "command_1"};
geolocation.sendCommand(requestInfo).then((result) => {
console.log('promise, sendCommand: ' + JSON.stringify(result));
});
```
## 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|
| -------- | -------- | -------- |
| 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.|
## 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.|
## 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.|