Reputation: 524
I am implementing a module which required user latitude and longitude to punch attendance offline.
I have implemented GPSTracker class followed this example on the LINK
But after enabling GPS location I am punching attendance then this class returns null location object. But after 30 to 60 sec It returns location object correctly.
I have also added COARSE
and FINE
permission in Manifest and get run time permission also.
So I need help how to get latitude and longitude instantly after enabling GPS location.
Upvotes: 5
Views: 1782
Reputation: 910
This code working after GPS on give location instantly
private void getLastLocation()
{
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
Log.e("location",location.getLatitude()+"");
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
locationManager.requestSingleUpdate(LocationManager.NETWORK_PROVIDER,locationListener,null);
}
Upvotes: 4
Reputation: 887
After enabling GPS location mFusedLocationClient.getLastLocation()
return null because it don`t have lastLocation. If you need to get location right after enabling you need to make self requestLocationUpdates like:
mFusedLocationClient.requestLocationUpdates(getLocationRequest(), mLocationCallback, null);
mLocationCallback from com.google.android.gms.location.LocationCallback;
and get result in callback like:
mLocationCallback = new LocationCallback() {
@SuppressLint("MissingPermission")
@Override
public void onLocationResult(LocationResult locationResult) {
if (locationResult == null) {
mFusedLocationClient.removeLocationUpdates(mLocationCallback);
return;
}
for (Location location : locationResult.getLocations()) {
// Update UI with location data
myCoordinate = new LatLng(location.getLatitude(), location.getLongitude());
}
mFusedLocationClient.removeLocationUpdates(mLocationCallback);
}
};
PS. in my case it work perfectly on all devices but not on samsung s8 and s9
Upvotes: 3
Reputation: 3444
Try with this:
FusedLocationProviderClient mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(getActivity());
private void getDeviceLocation() {
try {
Task<Location> locationResult = mFusedLocationProviderClient.getLastLocation();
locationResult.addOnCompleteListener(getActivity(), new OnCompleteListener<Location>() {
@Override
public void onComplete(@NonNull Task<Location> task) {
if (task.isSuccessful()) {
Location mLastKnownLocation = task.getResult();
Log.i(TAG, "==lat "+mLastKnownLocation.getLatitude());
Log.i(TAG, "==log "+mLastKnownLocation.getLongitude());
} else {
Log.d(TAG, "Current location is null. Using defaults.");
Log.e(TAG, "Exception: %s", task.getException());
}
}
});
} catch (SecurityException e) {
Log.e("Exception: %s", e.getMessage());
}
}
Upvotes: 0