Beeing Jk
Beeing Jk

Reputation: 3926

Android: Google Fused Location doesn't work while location is turned off

getLastLocation() returns NULL while phone location is Off on my 6.0 Nexus 7 tablet, but it works without GPS on another device. Why is it so and any way to solve it? I want to keep GPS off and just use network to get location.

Below is the class I am using to get location:

public class GPSCenter {
public static GoogleApiClient mGoogleApiClient;
public static Location mLastLocation;
private static Context mContext;

static GoogleApiClient.ConnectionCallbacks ccb = new GoogleApiClient.ConnectionCallbacks() {

    @Override
    public void onConnectionSuspended(int arg0) {
    }

    @Override
    public void onConnected(Bundle arg0) {

        if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }

        LocationRequest mLocationRequest = LocationRequest.create();
        mLocationRequest.setPriority(LocationRequest.PRIORITY_NO_POWER);

        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
    }
};
static GoogleApiClient.OnConnectionFailedListener odfl = new GoogleApiClient.OnConnectionFailedListener() {
    @Override
    public void onConnectionFailed(ConnectionResult arg0) {
    }
};

public static synchronized void buildGoogleApiClient(Context c) {
    try {

        mContext = c;
        mGoogleApiClient = new GoogleApiClient.Builder(c)
                .addConnectionCallbacks(ccb)
                .addOnConnectionFailedListener(odfl)
                .addApi(LocationServices.API)
                .build();

        mGoogleApiClient.connect();
    } catch (Exception ex) {
        Log.i("location", "error " + ex.toString());
        ex.printStackTrace();
    }
}

public static double getLatitude(Context c) {
    try {
        return mLastLocation.getLatitude();
    } catch (Exception ex) {
        return 0.0;
    }
}

public static double getLongitude(Context c) {
    try {
        return mLastLocation.getLongitude();
    } catch (Exception ex) {
        return 0.0;
    }
}
}

Upvotes: 1

Views: 7345

Answers (4)

Beeing Jk
Beeing Jk

Reputation: 3926

It isn't because of the GPS thing but, the Location Settings is not enabled yet in the device, it is under your device Settings>Google>Services>Location, or Settings>Privacy&security>Location, or just Settings>Location. I got confused because in some device the drop-down status bar shows GPS enabling button and some shows Location enabling button. There is a way to enable the Location Settings automatically (without navigating user to device settings), it solved my issue, hope it helps you:

After GoogleApiClient is connected, do a Location Settings checking as below:

public static GoogleApiClient mGoogleApiClient;
private static PendingResult<LocationSettingsResult> result;
private static LocationSettingsRequest.Builder builder;
public static Location mLastLocation;
public static boolean isLocationON = false;

public static synchronized void buildGoogleApiClient(Activity a, Context c) {

    try {

        mActivity = a;
        mContext = c;
        mGoogleApiClient = new GoogleApiClient.Builder(c)
                .addConnectionCallbacks(ccb)
                .addOnConnectionFailedListener(odfl)
                .addApi(LocationServices.API)
                .build();

        builder = new LocationSettingsRequest.Builder()
                .addLocationRequest(locationRequest);
        builder.setAlwaysShow(true);

        mGoogleApiClient.connect();

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

private static GoogleApiClient.ConnectionCallbacks ccb = new GoogleApiClient.ConnectionCallbacks() {

    @Override
    public void onConnected(Bundle arg0) {
        checkLocationSettings();
    }

    @Override
    public void onConnectionSuspended(int arg0) {
    }
};

private static void checkLocationSettings() {

    result = LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());

    result.setResultCallback(new ResultCallback<LocationSettingsResult>() {

        @Override
        public void onResult(@NonNull LocationSettingsResult locationSettingsResult) {
            final Status status = locationSettingsResult.getStatus();
            switch (status.getStatusCode()) {
                case LocationSettingsStatusCodes.SUCCESS:

                    isLocationON = true;

                    if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                        return;
                    }
                    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

                    if (mLocationSettingsListener != null) {
                        mLocationSettingsListener.onLocationON();
                    }

                    break;

                case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:

                    isLocationON = false;

                    try {
                        // This line will check the result and prompt a dialog if the device location settings is not enabled
                        status.startResolutionForResult(mActivity, REQUEST_CHECK_SETTINGS);

                    } catch (IntentSender.SendIntentException e) {
                    }
                    break;

                case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                    isLocationON = false;
                    // Location settings are unavailable so not possible to show any dialog now
                    if (mLocationSettingsListener != null) {
                        mLocationSettingsListener.onLocationError();
                    }
                    break;
            }
        }
    });
}

Upvotes: 2

Ali Kazi
Ali Kazi

Reputation: 1636

You have to set Location accuracy to Device Only in the emulator. High Accuracy or Battery Saving will not work with FusedLocationProviderApi in the emulator.

Upvotes: 0

Pararth
Pararth

Reputation: 8134

Th following explains the not returning any location value part:
LocationRequest.PRIORITY_NO_POWER:

No locations will be returned unless a different client has requested location updates in which case this request will act as a passive listener to those locations.

For FusedLocationProvider,

no explicitly unique source is specified anywhere within documentation.

So for obtaining the location without enabling GPS, you can trust PRIORITY_BALANCED_POWER_ACCURACY. From what i have observed, only PRIORITY_HIGH_ACCURACY uses GPS in a mandatory fashion.

A tablet could be wifi-only or not have a sim which, unlike other devices, rules out the scenario of location being obtained by other apps – maybe using the network.

Upvotes: 0

user6615010
user6615010

Reputation: 122

Through Google Api i think it's not possible. for that you need to use some api for ip geolocation or you may use MNC-Mobile network location or MCC-Mobile country code through this you can get. It may help you.

Upvotes: 0

Related Questions