0

i have implemented Google Play Service's Fused Location Api in my app to get continuously user Location. Everything works fine when wifi is on

but when i turn off wifi then it update user location using cellular info so the accuracy is more then 700meters.

what i want is when wifi is off then get user location from GPS.

 protected synchronized void buildGoogleApiClient() {
    Log.i(TAG, "Building GoogleApiClient");
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
    createLocationRequest();
}

protected void createLocationRequest() {
    mLocationRequest = new LocationRequest();


    mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);


    mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);

    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}

 @Override
public void onConnected(Bundle connectionHint) {
    Log.i(TAG, "Connected to GoogleApiClient");


    if (mCurrentLocation == null) {
        mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        mLastUpdateTime = DateFormat.getTimeInstance().format(new Date());
        updateUI();
    }

    if (mRequestingLocationUpdates) {
        startLocationUpdates();
    }
}


@Override
public void onLocationChanged(Location location) {
    mCurrentLocation = location;
    mLastUpdateTime = DateFormat.getTimeInstance().format(new Date());
    updateUI();
    Toast.makeText(this, getResources().getString(R.string.location_updated_message),
            Toast.LENGTH_SHORT).show();
}

1 Answers1

0

Apps that use location services must request location permissions. Android offers two location permissions:ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION. The permission you choose determines the accuracy of the location returned by the API.

In other word, the permission you choose decide the providers that will be used in order to determine the location then affect to the accuracy also, like this: ACCESS_FINE_LOCATION -> Gps, Cell, Wifi ACCESS_COARSE_LOCATION -> Cell and Wifi only

The accuracy depend on the provider. But combine with the priority LocationRequest.PRIORITY_HIGH_ACCURACY -> your app will likely use GPS to determine your location.

Lạng Hoàng
  • 1,790
  • 3
  • 17
  • 32