目录

Developing the High-precision Location Service

  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.

Overview

To request high-precision locations (accurate to within one meter), you need to apply for the location permission for your app, call the requestLocationUpdatesEx method, and set the LocationRequest object. For details, please refer to Requesting High-Precision Locations. You can specify a location mode as needed to continuously obtain location updates. To stop requesting location updates, call the removeLocationUpdates method.

Restrictions

The restrictions for using the high-precision location service are as follows:

To use APIs of the high-precision location service, ensure that you have downloaded and installed HMS Core (APK) on your device, and integrated the Location SDK into your project. For details, please refer to Preparations. This section provides only relevant code snippets. For the complete code, please refer to Sample Code.

Assigning App Permissions

  1. Declare the required permissions in the AndroidManifest.xml file. The Android OS provides two location permissions: ACCESS_COARSE_LOCATION (approximate location permission) and ACCESS_FINE_LOCATION (precise location permission).
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
  2. (Optional) If your app needs to continuously locate the device when it runs in the background in Android 10 or later, declare the ACCESS_BACKGROUND_LOCATION permission in the AndroidManifest.xml file.
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>

    The preceding permissions are dangerous permissions. You need to dynamically apply for these permissions (according to dangerous permission requirements in Android 6.0 or later).

Creating a Fused Location Provider Client

Create a FusedLocationProviderClient instance using the onCreate() method of the location service activity in your project, and use the instance to call location-related APIs.
Java
// Define a fusedLocationProviderClient object.
private FusedLocationProviderClient fusedLocationProviderClient; 
protected void onCreate(Bundle savedInstanceState) {   
    super.onCreate(savedInstanceState);
    fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this); 
}
Kotlin
// Define a fusedLocationProviderClient object.
private lateinit var fusedLocationProviderClient: FusedLocationProviderClient 
override fun onCreate(savedInstanceState: Bundle?) {   
    super.onCreate(savedInstanceState)
    fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this) 
}

Requesting High-Precision Locations

You can call the requestLocationUpdatesEx method to request high-precision locations from HMS Core (APK). The obtained locations will be returned using onLocationResult() in LocationCallback. To obtain the detailed location object, call locationResult.getLocations() or locationResult.getHWLocationList() to obtain a list of Location or HWLocation objects from the location callback result.

  1. Set high-precision location request parameters.
    Construct a LocationRequest object to request high-precision locations only when priority is set to LocationRequest.PRIORITY_HD_ACCURACY.
    Java
    LocationRequest mLocationRequest = new LocationRequest();
    // Set the location update interval, in milliseconds.
    mLocationRequest.setInterval(10000);
    // Set the location type to high-precision location.
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HD_ACCURACY);
    Kotlin
    val mLocationRequest = LocationRequest()
    // Set the location update interval, in milliseconds.
    mLocationRequest.interval = 10000
    // Set the location type to high-precision location.
    mLocationRequest.priority = LocationRequest.PRIORITY_HD_ACCURACY
  2. Define the location update callback.
    Java
    LocationCallback mLocationCallback;        
    mLocationCallback = new LocationCallback() {        
        @Override        
        public void onLocationResult(LocationResult locationResult) {        
            if (locationResult != null) {        
                // Obtain the location information.
                List<Location> locations = locationResult.getLocations();
                for (Location location : locations) {
                    // TODO: Process the callback result.
                }
            }        
        }        
    };
    Kotlin
    val mLocationCallback: LocationCallback
    mLocationCallback = object : LocationCallback() {
        override fun onLocationResult(locationResult: LocationResult) {
            if (locationResult != null) {
                // Obtain the location information.
                val locations =
                    locationResult.locations
                for (location in locations) {
                    // TODO: Process the callback result.
                }
            }
        }
    }
  3. Call requestLocationUpdatesEx to request high-precision locations.
    Java
    fusedLocationProviderClient        
        .requestLocationUpdatesEx(mLocationRequest, mLocationCallback, Looper.getMainLooper())        
        .addOnSuccessListener(new OnSuccessListener<Void>() {        
            @Override        
            public void onSuccess(Void aVoid) {        
                // Processing when the API call is successful.
            }        
        })
        .addOnFailureListener(new OnFailureListener() {        
            @Override        
            public void onFailure(Exception e) {        
               // Processing when the API call fails.
            }        
        });
    Kotlin
    fusedLocationProviderClient
        .requestLocationUpdatesEx(mLocationRequest, mLocationCallback, Looper.getMainLooper())
        .addOnSuccessListener(OnSuccessListener<Void?> {
            // Processing when the API call is successful.
        })
        .addOnFailureListener(OnFailureListener {
            // Processing when the API call fails.
        })
  4. (Optional) Call removeLocationUpdates() to stop requesting high-precision locations.
    Java
    // Note: When you stop requesting location updates, mLocationCallback must be the same object as LocationCallback passed to the requestLocationUpdatesEx method.
    fusedLocationProviderClient.removeLocationUpdates(mLocationCallback)        
        // Define callback for success in stopping requesting location updates.
        .addOnSuccessListener(new OnSuccessListener<Void>() {        
            @Override        
            public void onSuccess(Void aVoid) {      
               // TODO: Define callback for success in stopping requesting location updates.
            }        
        })
        // Define callback for failure in stopping requesting location updates.
        .addOnFailureListener(new OnFailureListener() {        
            @Override        
            public void onFailure(Exception e) {      
               // TODO: Define callback for failure in stopping requesting location updates.
            }        
        });
    Kotlin
    // Note: When you stop requesting location updates, mLocationCallback must be the same object as LocationCallback passed to the requestLocationUpdatesEx method.
    fusedLocationProviderClient.removeLocationUpdates(mLocationCallback)
        // Define callback for success in stopping requesting location updates.
        .addOnSuccessListener(OnSuccessListener<Void?> {
            // TODO: Define callback for success in stopping requesting location updates.
        })
        // Define callback for failure in stopping requesting location updates.
        .addOnFailureListener(OnFailureListener {
            // TODO: Define callback for failure in stopping requesting location updates.
        })