Android – there are so many ways to get the last location

 mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .addApi(LocationServices.API)
                    .build();

LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
Location lastKNownLocation = locationManager.getLastKNownLocation(locationProvider);

mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
mFusedLocationClient.getLastLocation()
        .addOnSuccessListener(this, new OnSuccessListener<Location>() {
            @Override
            public void onSuccess(Location location) {
                // Got last kNown location. In some rare situations, this can be null.
                if (location != null) {
                    // Logic to handle location object
                }
            }
        });

resolvent:

LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
Location lastKNownLocation = locationManager.getLastKNownLocation(locationProvider);

 mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
 mFusedLocationClient.getLastLocation()
    .addOnSuccessListener(this, new OnSuccessListener<Location>() {
        @Override
        public void onSuccess(Location location) {
            // Got last kNown location. In some rare situations this can be null.
            if (location != null) {
                // Logic to handle location object
            }
        }
    });

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>