2

I am using Google Service API , LocationServices.FusedLocationApi to find user's current and then updated Location. I have tested on emulator as well as on actual device and I have found that if I turn off GPS LocationServices.FusedLocationApi.getLastLocation() always returns null, however I get a valid value if I turn on the GPS. Here is the code which I am using:

private GoogleApiClient mGoogleApiClient;
private Location mLastKnownLocation;
mLastKnownLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

if (mLastKnownLocation != null) {
    Log.i(TAG, String.valueOf(mLastKnownLocation.getLatitude()));
    Log.i(TAG, String.valueOf(mLastKnownLocation.getLongitude()));
}

LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, createLocationRequest(), this);

Am I missing something here? Thanks in advance.

Pratik
  • 952
  • 2
  • 16
  • 31

1 Answers1

2

I think u should check if the your device has a GPS first by using this:

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            buildAlertMessageNoGps();
        }

 private void buildAlertMessageNoGps() {
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Your GPS seems to be disabled, do you want to enable it?")
                .setCancelable(false)
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
                        startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                    }
                })
                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
                        dialog.cancel();
                    }
                });
        final AlertDialog alert = builder.create();
        alert.show();
    }

And then use the LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient).

For more info and code please refer here.

bjiang
  • 6,068
  • 2
  • 22
  • 35
  • @bijang Thanks for the answer and detailed code. I am trying to understand that if GPS is disabled then what happens? Currently if GPS is disabled then it does not show my location, how can I fix this? – Pratik Oct 15 '15 at 17:39
  • Two ways to get your current location: 1. From `GPS`, you need to have GPS and enable it. 2. From `Network Location Provider`, but this is not accuracy than GPS. For more details, please refer [here](http://javapapers.com/android/get-current-location-in-android/). – bjiang Oct 15 '15 at 17:59
  • I got the problem, I was under impression that network location provider would work even if location is disabled. but that is not the case. Location has to be enabled if I have to find location (Different mode of location). Thanks for the help – Pratik Oct 16 '15 at 06:29