0

I am using Fused Location Api for getting the latitude and longitude.It's mentioned in the documents that it uses GPS, Wifi and network(Cell) to return the most accurate location of the user. I've a couple of questions here:

1)I've noticed that if I turn off the wifi, it still gives the result but it gives null when GPS is turned off, why is that? Isn't it suppose to use the Wifi to give the location detail even if gps is off or not available? P.S. I've used ACCESS_FINE_LOCATION for permission.

2)How can I know/test whether the location detail is returned using Wifi or GPS or network (Cell tower)?

3)Will it work using network (cell tower) when both GPS and wifi are off?

Fused Location Provider code

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    lblLocation = (TextView) findViewById(R.id.lblLocation);
    btnShowLocation = (Button) findViewById(R.id.btnShowLocation);
    btnStartLocationUpdates = (Button) findViewById(R.id.btnLocationUpdates);

    if (checkPlayServices()) {
        Log.i("playservice", checkPlayServices()+"");
        // Building the GoogleApi client
        buildGoogleApiClient();
    }

    btnShowLocation.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            displayLocation();
        }
    });

}

private void displayLocation() {

    mLastLocation = LocationServices.FusedLocationApi
            .getLastLocation(mGoogleApiClient);

    if (mLastLocation != null) {
        double latitude = mLastLocation.getLatitude();
        double longitude = mLastLocation.getLongitude();

        lblLocation.setText(latitude + ", " + longitude);
    } else {
        lblLocation.setText("(Couldn't get the location. Make sure location is enabled on the device)");
    }
}

protected synchronized void buildGoogleApiClient() {
    Log.i(TAG, "Building GoogleApiClient");

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
}

private boolean checkPlayServices() {
    int resultCode = GooglePlayServicesUtil
            .isGooglePlayServicesAvailable(this);
    if (resultCode != ConnectionResult.SUCCESS) {
        if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
            GooglePlayServicesUtil.getErrorDialog(resultCode, this,
                    PLAY_SERVICES_RESOLUTION_REQUEST).show();
        } else {
            Toast.makeText(getApplicationContext(),
                    "This device is not supported.", Toast.LENGTH_LONG)
                    .show();
            finish();
        }
        return false;
    }
    return true;
}

@Override
public void onConnected(Bundle bundle) {

    Log.i(TAG, "GoogleApiClient connected!");
    createLocationRequest();
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
    Log.i(TAG, " Location: " + mLastLocation);
}

protected void createLocationRequest() {

    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(10000);
    mLocationRequest.setFastestInterval(5000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, new LocationCallback() {
        @Override
        public void onLocationResult(final LocationResult locationResult) {
            Log.i("onLocationResult",locationResult + "");

            btnShowLocation.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    lblLocation.setText(locationResult.getLocations() + "");
                }
            });
        }

        @Override
        public void onLocationAvailability(LocationAvailability locationAvailability) {
            Log.i("onLocationAvailability", "onLocationAvailability: isLocationAvailable =  " + locationAvailability.isLocationAvailable());
        }
    }, null);
}

@Override
public void onConnectionSuspended(int i) {
    mGoogleApiClient.connect();
}

@Override
public void onConnectionFailed(ConnectionResult result) {
    Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = "
            + result.getErrorCode());
}


@Override
protected void onStart() {
    super.onStart();

    if (mGoogleApiClient != null) {
        mGoogleApiClient.connect();
        Log.i(TAG, "mGoogleApiClient.connect()");
    }
}

When I've GPS on, the output I get is:

I/onLocationResult(7481): LocationResult[locations: [Location[fused 27.673576,85.314083 acc=16 et=+1d1h59m30s347ms alt=1289.0999755859375 vel=0.93 bear=166.0]]]
Amrita Stha
  • 2,973
  • 3
  • 25
  • 46
  • Goto Settings -> Location -> Mode -> set it to 'High accuracy'. And then check if the result is null after turning off GPS. – Saurabh Thorat Aug 06 '17 at 11:47
  • I didn't find location mode->high accuracy in the setting. Instead I've set Location method to "GPS, WiFi and mobile networks" (Samsung mobile) but with no success. – Amrita Stha Aug 07 '17 at 05:09

0 Answers0