'LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient)' not returning any location if GPS is off

614 views Asked by At

I'm trying to get user's current location and for that I'm following this tutorial.

I'm running the following code on a device running Android Lollipop so there's no issue of runtime permission here.

Here's my code (Update 1.0):

@Override
    public void onConnected(@Nullable Bundle bundle) {

    Log.d("onConnected", "called");

    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(1000);
    mLocationRequest.setFastestInterval(1000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

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

   }

@Override
    public void onLocationChanged(Location location) {
        mLastLocation = location;
        if (mLastLocation != null) {
            currentLatDouble = mLastLocation.getLatitude();
            currentLngDouble = mLastLocation.getLongitude();

            Log.d("cltLC", String.valueOf(currentLatDouble));
            Log.d("clntLC", String.valueOf(currentLngDouble));
        }
    }

here onConnected is getting printed out but clt and clnt is not.

Here's permissions:

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

Here's build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.0"

    defaultConfig {
        applicationId "com.xxx.aaa"
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
        vectorDrawables.useSupportLibrary = true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    repositories {
        maven {
            url "https://jitpack.io"
        }
    }
    dexOptions {
        javaMaxHeapSize "4g"
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:25.1.0'
    compile 'com.android.support:design:25.1.0'
    compile 'com.google.android.gms:play-services:10.0.1'
    compile 'com.google.android.gms:play-services-location:10.0.1'
    compile 'com.google.firebase:firebase-database:10.0.1'
    compile 'com.firebase:geofire-android:2.1.0'
}

apply plugin: 'com.google.gms.google-services'

I don't want user to turn on GPS at this stage and want to retrieve current location without it.

I updated the code after seeing this answer, but again nothing is happening if the GPS is OFF!

Please let me know what's the issue here.

0

There are 0 answers