目录

LocationRequest

Class Info

public class LocationRequest implements Parcelable, IMessageEntity

Location request information class. An instance of this class will be returned when the create() method in this class is called.

The following sample code constructs a LocationRequest object:
Java
LocationRequest mLocationRequest;
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(5000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
Kotlin
val mLocationRequest: LocationRequest
mLocationRequest = LocationRequest()
mLocationRequest.interval = 5000
mLocationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY

Public Field Summary

Qualifier and Type

Field and Description

static final Creator<LocationRequest>

CREATOR

Builder of LocationRequest.

static final int

PRIORITY_BALANCED_POWER_ACCURACY

Used to request the block-level location.

static final int

PRIORITY_HD_ACCURACY

Used to request the high-precision location. Currently, this parameter is available only for the requestLocationUpdatesEx method.

CAUTION:
  1. To use the high-precision location capability, you need to comply with relevant laws and regulations. In addition, you and Huawei need to apply to related departments in China together for the license for using the high-precision location deflection plugin.
  2. Currently, the high-precision location capability is available only in Shenzhen, Guangzhou, Suzhou, Hangzhou, Chongqing, Chengdu, Tianjin, and Dongguan in the Chinese mainland. More cities will be supported in the future.

static final int

PRIORITY_HIGH_ACCURACY

Used to request the most accurate location.

static final int

PRIORITY_LOW_POWER

Used to request the city-level location.

static final int

PRIORITY_NO_POWER

Used to request the location with the optimal accuracy without additional power consumption.

static final int

PRIORITY_INDOOR

Used to request the indoor location. Currently, this field can only be used by the requestLocationUpdatesEx method.

static final int

PRIORITY_HIGH_ACCURACY_AND_INDOOR

Used to request the indoor location and fused location. Currently, this field can only be used by the requestLocationUpdatesEx method.

Public Constructor Summary

Constructor Name

LocationRequest()

Builds a LocationRequest instance.

Public Method Summary

Qualifier and Type

Method Name and Description

LocationRequest

create()

Creates a LocationRequest object.

boolean

isFastestIntervalExplicitlySet()

Checks whether the fastest request interval is used.

LocationRequest

setCountryCode(String countryCode)

Sets the country code.

LocationRequest

setExpirationDuration(long millis)

Sets the request expiration duration, in milliseconds.

LocationRequest

setExpirationTime(long millis)

Sets the request expiration time, in milliseconds.

LocationRequest

setFastestInterval(long millis)

Sets the fastest interval for requesting location updates, in milliseconds. The values of Interval and FastestInterval, whichever is smaller, will be used for requesting location updates. If fastestInterval needs to be set, you are advised to set it to a value far less than that of Interval.

LocationRequest

setInterval(long millis)

Sets the interval for requesting location updates, in milliseconds.

LocationRequest

setLanguage(String language)

Sets the language.

LocationRequest

setMaxWaitTime(long millis)

Sets the maximum waiting time for location updates, in milliseconds.

LocationRequest

setNeedAddress(boolean needAddress)

Sets whether to return the address information.

LocationRequest

setNumUpdates(int numUpdates)

Sets the number of location updates. If the value is 3, the app will receive three location updates.

LocationRequest

setPriority(int priority)

Sets the location type.

CAUTION:
  1. To use the high-precision location capability, you need to comply with relevant laws and regulations. In addition, you and Huawei need to apply to related departments in China together for the license for using the high-precision location deflection plugin.
  2. Currently, the high-precision location capability is available only in Shenzhen, Guangzhou, Suzhou, Hangzhou, Chongqing, Chengdu, Tianjin, and Dongguan in the Chinese mainland. More cities will be supported in the future.

LocationRequest

setSmallestDisplacement(float smallestDisplacement)

Sets the smallest displacement between two locations, in meters.

Public Fields

[h2]CREATOR

Fields

public static final Creator<LocationRequest> CREATOR

Builder of LocationRequest, which is used to deserialize location request information.

[h2]PRIORITY_BALANCED_POWER_ACCURACY

Fields

public static final int PRIORITY_BALANCED_POWER_ACCURACY

Used to request the block-level location.

PRIORITY_BALANCED_POWER_ACCURACY: 102

For details about other constant values, please refer to Constant Values.

[h2]PRIORITY_HD_ACCURACY

  1. To use the high-precision location capability, you need to comply with relevant laws and regulations. In addition, you and Huawei need to apply to related departments in China together for the license for using the high-precision location deflection plugin.
  2. Currently, the high-precision location capability is available only in Shenzhen, Guangzhou, Suzhou, Hangzhou, Chongqing, Chengdu, Tianjin, and Dongguan in the Chinese mainland. More cities will be supported in the future.

Fields

public static final int PRIORITY_HD_ACCURACY

Used to request the high-precision location. Currently, this parameter is available only for the requestLocationUpdatesEx method.

PRIORITY_HD_ACCURACY: 200

For details about other constant values, please refer to Constant Values.

[h2]PRIORITY_HIGH_ACCURACY

Fields

public static final int PRIORITY_HIGH_ACCURACY

Used to request the most accurate location.

PRIORITY_HIGH_ACCURACY: 100

For details about other constant values, please refer to Constant Values.

[h2]PRIORITY_LOW_POWER

Fields

public static final int PRIORITY_LOW_POWER

Used to request the city-level location.

PRIORITY_LOW_POWER: 104

For details about other constant values, please refer to Constant Values.

[h2]PRIORITY_NO_POWER

Fields

public static final int PRIORITY_NO_POWER

Used to request the location with the optimal accuracy without additional power consumption.

PRIORITY_NO_POWER: 105

For details about other constant values, please refer to Constant Values.

[h2]PRIORITY_INDOOR

Fields

public static final int PRIORITY_INDOOR

Used to request indoor location.

PRIORITY_INDOOR: 300

For details about other constant values, please refer to Constant Values.

[h2]PRIORITY_HIGH_ACCURACY_AND_INDOOR

Fields

public static final int PRIORITY_HIGH_ACCURACY_AND_INDOOR

Used to request the indoor location and fused location.

PRIORITY_HIGH_ACCURACY_AND_INDOOR: 400

For details about indoor location constraints, please refer to Developing the Indoor Location Service. For other constant values, please refer to Constant Values.

Public Constructors

[h2]LocationRequest

Constructor

public LocationRequest()

Builds a LocationRequest instance.

Public Methods

[h2]create

Method

public static LocationRequest create()

Creates a LocationRequest object.

Returns

Type

Description

LocationRequest

Location request class.

[h2]isFastestIntervalExplicitlySet

Method

public boolean isFastestIntervalExplicitlySet()

Checks whether the fastest request interval is used.

Returns

Type

Description

boolean

true if the fastest request interval is used; false otherwise.

[h2]setCountryCode

Method

public LocationRequest setCountryCode(String countryCode)

Sets the country code.

Parameters

Name

Description

countryCode

Country code. The value is a two-letter code complying with the ISO 3166-1 standard. By default, this parameter is left empty. Currently, the options include only EN and CN.

Returns

Type

Description

LocationRequest

Location request.

[h2]setExpirationDuration

Method

public LocationRequest setExpirationDuration(long millis)

Sets the request expiration duration. Different from expirationTime, the ExpirationDuration parameter is irrelevant to the boot time. If the location update needs to be stopped 200 seconds later, the request expiration duration should be 200000.

Parameters

Name

Description

millis

Request expiration duration, in milliseconds. The default value is Long.MAX_VALUE.

Returns

Type

Description

LocationRequest

Location request.

[h2]setExpirationTime

Method

public LocationRequest setExpirationTime(long millis)

Sets the request expiration time since boot, in milliseconds. You can use the SystemClock.elapsedRealtime() method to obtain the number of milliseconds elapsed from the boot time to the current time. If the location update needs to be stopped 200 seconds later, the request expiration time should equal the return result of SystemClock.elapsedRealtime() plus 200000.

Parameters

Name

Description

millis

Request expiration time, in milliseconds. The default value is Long.MAX_VALUE.

Returns

Type

Description

LocationRequest

Location request.

[h2]setFastestInterval

Method

public LocationRequest setFastestInterval(long millis)

Sets the fastest interval for requesting location updates. The default value is one sixth of Interval.

Parameters

Name

Description

millis

Fastest request interval, in milliseconds. The default value is 600000. If another app initiates a location request, the location is also reported to that app at the interval specified in fastestInterval.

Returns

Type

Description

LocationRequest

Location request.

Throws

Name

Description

IllegalArgumentException

Invalid parameter. The value of millis is less than 0. Ensure that the parameter value is correct.

[h2]setInterval

Method

public LocationRequest setInterval(long millis)

Sets the interval for requesting location updates. If the value is 50000, the app will receive a location update every 50 seconds.

Parameters

Name

Description

millis

Interval for requesting location updates, in milliseconds. The default value is 3600000.

Returns

Type

Description

LocationRequest

Location request.

Throws

Name

Description

IllegalArgumentException

Invalid parameter. The value of millis is less than 0. Ensure that the parameter value is correct.

[h2]setLanguage

Method

public LocationRequest setLanguage(String language)

Sets the language.

Parameters

Name

Description

language

Language, which is a two-letter code complying with the ISO 639-1 standard. For example, en indicates English. By default, this parameter is left empty. Currently, the options include only en and zh.

Returns

Type

Description

LocationRequest

Location request.

[h2]setMaxWaitTime

Method

public LocationRequest setMaxWaitTime(long millis)

Sets the maximum waiting time for location updates, in milliseconds.

When the setting takes effect, the time specified by maxWaitTime will be used as the location update period. After the location update time is reached, locations in the period will be returned to the app at a time.

NOTE:
  • The setting takes effect only when the value of maxWaitTime is twice of or greater than twice of the interval for requesting location updates.
  • This setting may lead to slow first location callback. You are advised not to set this parameter unless necessary.

Parameters

Name

Description

millis

Maximum waiting time, in milliseconds.

Returns

Type

Description

LocationRequest

Location request.

[h2]setNeedAddress

Method

public LocationRequest setNeedAddress(boolean needAddress)

Sets whether to return the address information.

Parameters

Name

Description

needAddress

Indicates whether to return the address information. The default value is false.

Returns

Type

Description

LocationRequest

Location request.

[h2]setNumUpdates

Method

public LocationRequest setNumUpdates(int numUpdates)

Sets the number of location updates. If the value is 3, the app will receive three location updates.

Parameters

Name

Description

numUpdates

Number of requested location updates. The default value is Integer.MAX_VALUE.

Returns

Type

Description

LocationRequest

Location request.

Throws

Name

Description

IllegalArgumentException

Invalid parameter. The value of numUpdates is less than or equal to 0. Ensure that the parameter value is correct.

[h2]setPriority

  1. To use the high-precision location capability, you need to comply with relevant laws and regulations. In addition, you and Huawei need to apply to related departments in China together for the license for using the high-precision location deflection plugin.
  2. Currently, the high-precision location capability is available only in Shenzhen, Guangzhou, Suzhou, Hangzhou, Chongqing, Chengdu, Tianjin, and Dongguan in the Chinese mainland. More cities will be supported in the future.

Method

public LocationRequest setPriority(int priority)

Sets the location type. If the GNSS location is requested, set priority to 100. If the network location is requested, set priority to 102 or 104. If the location only needs to be passively received instead of being proactively requested, set priority to 105. If the high-precision location is requested, set priority to 200. If the indoor location is requested, set priority to 300.

Parameters

Name

Description

priority

Requested location type. The default value is 102.

Returns

Type

Description

LocationRequest

Location request.

Throws

Name

Description

IllegalArgumentException

Invalid parameter. Ensure that the value of priority is one of the location types defined by LocationRequest.

[h2]setSmallestDisplacement

Method

public LocationRequest setSmallestDisplacement(float smallestDisplacement)

Sets the smallest displacement for location updates. If the displacement between two locations is greater than or equal to the specified parameter value, a location update will be sent to the app.

Parameters

Name

Description

smallestDisplacement

Smallest displacement between two locations, in meters. The default value is 0.

Returns

Type

Description

LocationRequest

Location request.

Throws

Name

Description

IllegalArgumentException

Invalid parameter. The value of smallestDisplacement is less than 0.0. Ensure that the parameter value is correct.