App Crashes While Requesting Location Updates With The Help Of Fused Location
I was requesting the location updates with the help of LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, locationListener); But As I trie
Solution 1:
You have requested location updates with the help of locationListener
which you have not initialized. You can solve the problem either by initializing a location listener or by changing locationListener
to this
in the given statement of your code.
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, locationListener);
to:
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);
Solution 2:
Implementation of fused location service has changed, you can do that without the GoogleApiClient
. You can try like this,
Add required location library to
build.grade
implementation 'com.google.android.gms:play-services-location:15.0.0'
Add required permissions to manifest
<uses-permissionandroid:name="android.permission.ACCESS_COARSE_LOCATION"/><uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION"/>
Inside
Welcome
activity
publicclassWelcomeextendsAppCompatActivityimplementsOnMapReadyCallback {
...
...
privateFusedLocationProviderClient mFusedLocationClient;
privateLocationRequest mLocationRequest = LocationRequest.create()
.setInterval(1000)
.setFastestInterval(500)
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
privateLocationCallback mLocationCallback = newLocationCallback() {
@OverridepublicvoidonLocationResult(LocationResult result) {
super.onLocationResult(result);
for (Location location : result.getLocations())
if (location != null) {
displayLocation(result.getLastLocation());
break;
}
}
};
...
...
@OverrideprotectedvoidonCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
...
...
// Get fussed location instance
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
...
...
}
@OverridepublicvoidonMapReady(GoogleMap googleMap) {
...
...
// Check if location permission is grantedif (!hasLocationPermission()) {
// Request location permission
} else {
// Get the last known locationstartLocationUpdate();
}
}
privatevoidstartLocationUpdate() {
try {
mFusedLocationClient.getLastLocation().addOnSuccessListener(newOnSuccessListener<Location>() {
@OverridepublicvoidonSuccess(Location location) {
if (location != null) {
displayLocation(location);
} else {
mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, null);
}
}
});
} catch (SecurityException ex) {
ex.printStackTrace();
}
}
privatebooleanhasLocationPermission() {
returnActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED;
}
privatevoidstopLocationUpdate() {
mFusedLocationClient.removeLocationUpdates(mLocationCallback);
}
privatevoiddisplayLocation(Location location) {
// do something
...
...
}
}
Post a Comment for "App Crashes While Requesting Location Updates With The Help Of Fused Location"