How to get location instantly after gps location enable
Asked Answered
T

3

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.

Tautog answered 23/4, 2018 at 8:13 Comment(4)
There is no 'instant'. You can use Last Known Location.Nahshunn
Yes, I used getLastKnownLocation() method but it's not returning location instantly @CàphêđenTautog
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.Nahshunn
No, when you enable GPS and instantly called getLastKnownLocation() its return null after 30- 60 It returns location. I have tried it. @CàphêđenTautog
L
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);

}
Loverly answered 4/10, 2018 at 7:33 Comment(0)
S
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

Stroy answered 16/8, 2018 at 7:10 Comment(0)
C
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());
            }
     }
Cheapskate answered 23/4, 2018 at 8:23 Comment(5)
Thanks for the answer. This is not working for me. @CheapskateTautog
Did you find any answer yet? Please help me.Marabout
Found one solution it is working with Java not working with Kotlin. @MaraboutTautog
Please share with me @NivruttiPawarMarabout
use this library. github.com/mrmans0n/smart-location-lib@InnocentTautog

© 2022 - 2024 — McMap. All rights reserved.