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.
The restrictions for using the high-precision location service are as follows:
The high-precision location service is supported only on the Chinese mainland version of these device models.
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.
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<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).
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) }
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.
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
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. } } } }
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. })
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. })