How to get current latitude and longitude without using google maps android

1.9k views Asked by At

I am trying to get the current location of the user either by GPS or location provider but every solution I have tried (many from stacksoverflow and google, youtube as well) gives a null as latitude and longitude.

Here is the code I am using

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_COARSE);
        criteria.setAltitudeRequired(false);
        criteria.setSpeedRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(false);

        double lat = 0;
        double lng = 0;
        provider = locationManager.getBestProvider(criteria, false);

        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

            return;
        }
        Location location = locationManager.getLastKnownLocation(provider);
        if (location != null)
        {
            lat = location.getLatitude();
            lng = location.getLongitude();
            Toast.makeText(this,"Location"+lat+" "+lng+" ",Toast.LENGTH_LONG).show();

        }else
            Toast.makeText(this,"Location"+lat+" "+lng+" ",Toast.LENGTH_LONG).show();

The above code always gives 0.0 and 0.0 as lat and long.

I have also included the permissions in Android Manifest :

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

Any suggestions please?

2

There are 2 answers

0
Itzik Samara On
    public static void getLocation(final Context context, final Looper looper, final LocationUpdateListener locationUpdateListener, final LocationListener locationListener) {


    try {
        boolean isProviderEnabled;
        Location location;
        final LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

        isProviderEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        if (isProviderEnabled) {
            if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, locationListener, looper);
                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                            locationManager.removeUpdates(locationListener);
                        }
                        getBackupLocation(context, looper, locationUpdateListener, locationListener);
                    }
                }, 10000);

                return;

            }
        }
        isProviderEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        if (isProviderEnabled) {

            locationManager.requestSingleUpdate(LocationManager.NETWORK_PROVIDER, locationListener, looper);
            return;

        }
        location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        if (location != null) {
            if (locationUpdateListener != null) {
                locationUpdateListener.onLocationUpdate(location);
                locationManager.removeUpdates(locationListener);
            }
            return;
        }

        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (location != null) {
            if (locationUpdateListener != null) {
                locationUpdateListener.onLocationUpdate(location);
                locationManager.removeUpdates(locationListener);
            }
            return;

        }

        float defaultValue = -10000;
        SharedManager sharedManager = SharedManager.getInstance();
        double lat = sharedManager.getDoubleValue(SharedManager.LAT, defaultValue);
        double lng = sharedManager.getDoubleValue(SharedManager.LNG, defaultValue);

        if (lat != defaultValue && lng != defaultValue) {
            location = new Location(LocationManager.PASSIVE_PROVIDER);
            location.setLatitude(lat);
            location.setLongitude(lng);
            if (locationUpdateListener != null) {
                locationUpdateListener.onLocationUpdate(location);
                locationManager.removeUpdates(locationListener);
            }
            return;
        }
        location = new Location(LocationManager.PASSIVE_PROVIDER);
        location.setLatitude(PASSIVE_LAT);
        location.setLongitude(PASSIVE_LONG);
        if (locationUpdateListener != null) {
            locationUpdateListener.onLocationUpdate(location);
            locationManager.removeUpdates(locationListener);
        }

}
    catch(
Exception e)

    {
        //No Location
        Location location = new Location(LocationManager.PASSIVE_PROVIDER);
        location.setLatitude(PASSIVE_LAT);
        location.setLongitude(PASSIVE_LONG);
        if (locationUpdateListener != null) {
            locationUpdateListener.onLocationUpdate(location);
        }
    }

  }

Working function that checks GPS , Network and LastKnownLocation.. my Suggestion : First use LastKnownLocation then Netowrk and last Resort as GPS.. less Battery usage.

Location results return to the Listeners.

2
Radu Serse On

This is all I used for my project. Hope it helps!

Declarations:

Location location;
    LocationManager locationManager;
    String provider; 

onCreate:

  locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            provider = locationManager.getBestProvider(new Criteria(), false);

        locationManager.requestLocationUpdates(provider, 400, 1, this);

        location = locationManager.getLastKnownLocation(provider);

        lat = location.getLatitude(); long = location.getLongitude();