5

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.

R. García
  • 815
  • 9
  • 20
Nivrutti Pawar
  • 514
  • 3
  • 17
  • There is no 'instant'. You can use `Last Known Location`. – Cà phê đen Apr 23 '18 at 08:15
  • Yes, I used getLastKnownLocation() method but it's not returning location instantly @Càphêđen – Nivrutti Pawar Apr 23 '18 at 08:58
  • `getLastKnownLocation` should return immediately. The only thing you should concern is that it may be out-of-date. For the current location, you have to wait for the service becoming available. There's no other chance. – Cà phê đen Apr 24 '18 at 03:20
  • No, when you enable GPS and instantly called getLastKnownLocation() its return null after 30- 60 It returns location. I have tried it. @Càphêđen – Nivrutti Pawar Apr 24 '18 at 07:15

3 Answers3

4

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);

}
Tarun Umath
  • 900
  • 10
  • 7
3

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

Vadim Eksler
  • 865
  • 9
  • 24
0

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());
            }
     }
Deven
  • 3,078
  • 1
  • 32
  • 34