androidhuawei-mobile-serviceshuawei-location-kit

The getLastLocation API cannot obtain the Mate 30 device location


I am using Huawei Location Kit to obtain the device location and find out that the location result is unstable.

I have two test devices: Mate 30 and P30 Lite. When I use the getLastLocation() method to obtain the device location on Mate 30, the location result is null. However, the method can obtain the device location correctly on P30 Lite. The HMS Core (APK) has been updated to the 4.0.3.316 version on both test devices. The code for obtaining the device location is also the same for both test devices, as described below:

private void getLastLocation() {        
        try {            
            Task<Location> lastLocation = mFusedLocationProviderClient.getLastLocation();       
            lastLocation.addOnSuccessListener(new OnSuccessListener<Location>() {
                @Override
                public void onSuccess(Location location) {                    
                    if (location == null) {                        
                        LocationLog.i(TAG, "getLastLocation onSuccess location is null");
                    return;
                    }                    
                    LocationLog.i(TAG,                            
                            "getLastLocation onSuccess location[Longitude,Latitude]:" + location.getLongitude() + ","                                    
                                    + location.getLatitude());                
                }            
            }).addOnFailureListener(new OnFailureListener() {                
                @Override                
                public void onFailure(Exception e) {                    
                    LocationLog.e(TAG, "getLastLocation onFailure:" + e.getMessage());                
                }            
            });        
        } catch (Exception e) {
            LocationLog.e(TAG, "getLastLocation exception:" + e.getMessage());        
        }    
    }

Why is this happening?


Solution

  • The getLastLocation API obtains the last location in the cache of HMS Core. If no location is available in the cache of HMS Core, null is returned. Therefore, the possible cause is that the location function has never been used on the Mate 30 device or the cache of HMS Core has been cleared.

    You are advised not to use the location obtained using the getLastLocation API as the latest location because the API cannot obtain the latest location. It is recommended that requestLocationUpdate be used to obtain more precise location. For details, visit https://developer.huawei.com/consumer/cn/doc/development/HMS-Guides/location-guidev4#h2-1586931075472.

    When using requestLocationUpdate, you can set the callback cycle and times as needed. In addition, you can use getLastLocation as an alternative for obtaining the device location.