目录

Location

has.openLocation

has.openLocation(Object object)

Opens a location using the built-in map of the map engine.

Before using this component, make preparations by referring to Preparations.

Since: 1.0.0

Parameters

The input parameter is an object containing the following fields.

Parameter Type Mandatory Description
latitude number Yes

Latitude. The value ranges from –90 to 90. A negative value indicates a south latitude.

Note: For the Chinese mainland, Hong Kong (China), and Macao (China), use gcj02. For other countries and regions, use wgs84.

longitude number Yes

Longitude. The value ranges from –180 to 180. A negative value indicates a west longitude. The coordinate system to be used is determined by the value of type.

Note: For the Chinese mainland, Hong Kong (China), and Macao (China), use gcj02. For other countries and regions, use wgs84.

name string Yes Location name.
address string No Detailed address description.
success function No Callback triggered when the API call is successful.
fail function No Callback triggered when the API call fails.
complete function No Callback triggered when the API call is complete, regardless of success or failure.

Error codes

Error Code Error Message
101 context is undefined.
102 latitude is invalid.
80000101

openLocation error.

For details, see ArkTS API Error Codes.

Example:

has.openLocation({
  longitude: 114.52845090013192,
  latitude: 30.497287768914163,
  name: 'name',
  address: 'address',
  success: () => {
    console.info('openLocation success');
  },
  fail: (err) => {
    console.error('openLocation fail', err);
  },
  complete: (res) => {
    console.info('openLocation complete', res);
  }
});

has.getLocation

has.getLocation(Object object)

Obtains the current location and speed. Use of high-precision positioning may increase the API response time. You can set highAccuracyExpireTime to specify the timeout duration.

Since: 1.0.0

Required permissions:

  1. ohos.permission.LOCATION and ohos.permission.APPROXIMATELY_LOCATION, which need to be declared in module.json5.

  2. scope.userLocation permission, which is applied for using has.authorize().

Parameters

The input parameter is an object containing the following fields.

Parameter Type Default Value Mandatory Description
type string wgs84 No

- wgs84: GPS coordinate system.

- gcj02: coordinate system of Chinese State Bureau of Surveying and Mapping.

altitude boolean false No Whether to return the altitude information. If the value is set to true, the altitude information is returned. Note that this requires high-precision positioning and therefore may increase the API response time.
isHighAccuracy boolean false No Whether to enable high-precision positioning.
highAccuracyExpireTime number - No High-precision positioning timeout interval, in ms. The highest precision within the specified time is returned. High-precision positioning takes effect only when the value is greater than 3000 ms.
success function - No Callback triggered when the API call is successful.
fail function - No Callback triggered when the API call fails.
complete function - No Callback triggered when the API call is complete, regardless of success or failure.

Return value of success

Parameter Type Description
latitude number Latitude. The value range is [–90, 90]. A negative value indicates the Southern Hemisphere. The coordinate system to be used is determined by the value of type.
longitude number Longitude. The value range is [–180, 180]. A negative value indicates the Western Hemisphere. The coordinate system to be used is determined by the value of type.
speed number Speed, in m/s.
accuracy number Location accuracy, that is, the degree of proximity between the obtained location and the real location. For example, the value 10 indicates that the difference between the obtained location and the real location is 10 m. A smaller value indicates a higher accuracy.
altitude number Altitude, in meters.
verticalAccuracy number Vertical accuracy, in meters.

Error codes

Error Code Error Message
101 context is undefined.
103

1. authorize error.

2. has no permission.

80000201 getLocation error.

Example:

has.authorize({
  scope: 'scope.userLocation',
  success: () => {
    has.getLocation({
      type: 'wgs84',
      success: (res) => {
        console.info('getLocation success', res);
      },
      fail: (err) => {
        console.error('getLocation fail', err);
      },
      complete: (res) => {
        console.info('getLocation complete', res);
      }
    });
  }
});

has.getFuzzyLocation

has.getFuzzyLocation(Object object)

Obtains the current fuzzy location.

Since: 1.0.0

Required permissions:

  1. ohos.permission.APPROXIMATELY_LOCATION, which needs to be declared in module.json5.

  2. scope.userFuzzyLocation permission, which is applied for using has.authorize().

Parameters

The input parameter is an object containing the following fields.

Parameter Type Default Value Mandatory Description
type string wgs84 No Type of the coordinate system to use. The value wgs84 means the GPS coordinate system, and gcj02 means the coordinate system of Chinese State Bureau of Surveying and Mapping.
success function - No Callback triggered when the API call is successful.
fail function - No Callback triggered when the API call fails.
complete function - No Callback triggered when the API call is complete, regardless of success or failure.

Return value of success

Parameter Type Description
latitude number Latitude. The value range is [–90, 90]. A negative value indicates the Southern Hemisphere. The coordinate system to be used is determined by the value of type.
longitude number Longitude. The value range is [–180, 180]. A negative value indicates the Western Hemisphere. The coordinate system to be used is determined by the value of type.

Error codes

Error Code Error Message
101 context is undefined.
103

1. authorize error.

2. has no permission.

80000301 getFuzzyLocation error.

Example:

has.authorize({
  scope: 'scope.userFuzzyLocation',
  success: () => {
    has.getFuzzyLocation({
      type: 'wgs84',
      success: (res) => {
        console.info('getFuzzyLocation success', res);
      },
      fail: (err) => {
        console.error('getFuzzyLocation fail', err);
      },
      complete: (res) => {
        console.info('getFuzzyLocation complete', res);
      }
    })
  }
});

has.chooseLocation

has.chooseLocation(Object object)

Opens a map to select a location.

Before using this component, make preparations by referring to Preparations.

Since: 1.0.0

Parameters

The input parameter is an object containing the following fields.

Parameter Type Mandatory Description
latitude number No

Latitude of the destination location.

Note: For the Chinese mainland, Hong Kong (China), and Macao (China), use gcj02. For other countries and regions, use wgs84.

longitude number No

Longitude of the destination location.

Note: For the Chinese mainland, Hong Kong (China), and Macao (China), use gcj02. For other countries and regions, use wgs84.

success function No Callback triggered when the API call is successful.
fail function No Callback triggered when the API call fails.
complete function No Callback triggered when the API call is complete, regardless of success or failure.

Return value of success

Parameter Type Description
name string Location name.
address string Detailed address.
countryCode string Country/Region code.
countryName string Country name.
latitude number

Latitude. The value ranges from –90 to 90. A negative value indicates a south latitude.

Note: For the Chinese mainland, Hong Kong (China), and Macao (China), use gcj02. For other countries and regions, use wgs84.

longitude number

Longitude. The value ranges from –180 to 180. A negative value indicates a west longitude.

Note: For the Chinese mainland, Hong Kong (China), and Macao (China), use gcj02. For other countries and regions, use wgs84.

Error codes

Error Code Error Message
101 context is undefined.
102 atitude is invalid.
80000401 chooseLocation error.

Example:

has.chooseLocation({
  longitude: 114.52845090013192,
  latitude: 30.497287768914163,
  success: (res) => {
    console.info('chooseLocation success', res);
  },
  fail: (err) => {
    console.error('chooseLocation fail', err);
  },
  complete: (res) => {
    console.info('chooseLocation complete', res);
  }
});

has.startLocationUpdate

has.startLocationUpdate(Object object)

Enables location updates for an atomic service when it enters the foreground.

Since: 1.0.15

Required permissions:

  1. ohos.permission.APPROXIMATELY_LOCATION, which needs to be declared in module.json5.

  2. scope.userFuzzyLocation permission, which is applied for using has.authorize().

Parameters

The input parameter is an object containing the following fields.

Parameter Type Default Value Mandatory Description
type string gcj02 No If the value is wgs84, GPS coordinates are returned. If the value is gcj02, coordinates that can be used for has.openLocation are returned.
success function - No Callback triggered when the API call is successful.
fail function - No Callback triggered when the API call fails.
complete function - No Callback triggered when the API call is complete, regardless of success or failure.

Error codes

Error Code Error Message
101 context is undefined.
103

1. authorize error.

2. has no permission.

80000501 startLocationUpdate error.

Example:

has.startLocationUpdate({
  success: () => {
    console.info('startLocationUpdate success');
  },
  fail: (err) => {
    console.error('startLocationUpdate fail', err);
  },
  complete: (res) => {
    console.info('startLocationUpdate complete', res);
  }
});

has.startLocationUpdateBackground

has.startLocationUpdateBackground(Object object)

Enables location updates for an atomic service even when it enters the background.

Since: 1.0.15

Required permissions:

  1. ohos.permission.APPROXIMATELY_LOCATION, which needs to be declared in module.json5.

  2. scope.userFuzzyLocation permission, which is applied for using has.authorize().

  3. Declare the location permission in the backgroundModes field in module.json5.

    "module": {
      "abilities": [
        {
          "backgroundModes": [
            "location"
          ]
        }
      ]
    }

Parameters

The input parameter is an object containing the following fields.

Parameter Type Default Value Mandatory Description
type string gcj02 No If the value is wgs84, GPS coordinates are returned. If the value is gcj02, coordinates that can be used for has.openLocation are returned.
success function - No Callback triggered when the API call is successful.
fail function - No Callback triggered when the API call fails.
complete function - No Callback triggered when the API call is complete, regardless of success or failure.

Error codes

Error Code Error Message
101 context is undefined.
103

1. authorize error.

2. has no permission.

80000601 startLocationUpdateBackground error.

Example:

has.startLocationUpdateBackground({
  success: () => {
    console.info('startLocationUpdateBackground success');
  },
  fail: (err) => {
    console.error('startLocationUpdateBackground fail', err);
  },
  complete: (res) => {
    console.info('startLocationUpdateBackground complete', res);
  }
});

has.stopLocationUpdate

has.stopLocationUpdate(Object object)

Disables location updates for an atomic service. The atomic service will not receive any location update messages when running in the foreground or background.

Since: 1.0.15

Parameters

The input parameter is an object containing the following fields.

Parameter Type Mandatory Description
success function No Callback triggered when the API call is successful.
fail function No Callback triggered when the API call fails.
complete function No Callback triggered when the API call is complete, regardless of success or failure.

Error codes

Error Code Error Message
101

1. authorize fail.

2. context is undefined.

103 has no permission.
80000701 stopLocationUpdate error.

Example:

has.stopLocationUpdate({
  success: () => {
    console.info('stopLocationUpdate success');
  },
  fail: (err) => {
    console.error('stopLocationUpdate fail', err);
  },
  complete: (res) => {
    console.info('stopLocationUpdate complete', res);
  }
});

has.onLocationChange

has.onLocationChange(function callback)

Enables listening for real-time geographic location change events. This method must be used with has.startLocationUpdateBackground and has.startLocationUpdate.

Since: 1.0.15

Parameters

Parameter Type Mandatory Description
callback function Yes Listener function for real-time geographic location changes.

Return value of callback

An object containing the following fields.

Parameter Type Description
latitude number Latitude. The value ranges from –90 to 90. A negative value indicates a south latitude. The GCJ-02 coordinate system is used.
longitude number Longitude. The value ranges from –180 to 180. A negative value indicates a west longitude. The GCJ-02 coordinate system is used.
speed number Speed, in m/s.
accuracy number Location precision.
altitude number Altitude, in meters.
verticalAccuracy number Vertical accuracy, in meters.
horizontalAccuracy number Horizontal accuracy, in meters.

Example:

const onLocationChangeFn = function(res) {
  console.info('onLocationChange callback triggered', res);
};
has.onLocationChange(onLocationChangeFn);

has.offLocationChange

has.offLocationChange(function callback)

Disables listening for real-time geographic location changes.

Since: 1.0.15

Parameters

Parameter Type Mandatory Description
callback function No Listener function passed by has.onLocationChange. If this parameter is not passed, all listener functions are removed.

Example:

const onLocationChangeFn = function(res) {
  console.info('onLocationChange callback triggered', res);
};
has.onLocationChange(onLocationChangeFn);
has.offLocationChange(onLocationChangeFn);