Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fused Location Provider not getting location unless GPS is on

So I have implemented the new Fused Location Provider API to get a location of the user but for some reason, I cannot get any location unless the GPS is on. Not always, will users have their GPS on and I would like to not have to ask them to turn their GPS on every time the load the app. How can I tell the API to give me a location with whatever provider it has available?

Here is my code:

public class FusedLocationService implements
        LocationListener,
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener {

    public interface OnLocationChangedListener {
        public void OnLocationChanged(Location location);
    }

    private final String TAG = "SE8611";
    private boolean mRequestingLocationUpdates = true;

    private OnLocationChangedListener mCallBack;

    Service locationService;
    private LocationRequest locationRequest;
    private GoogleApiClient googleApiClient;
    private Location mCurrentLocation;
    private FusedLocationProviderApi fusedLocationProviderApi = LocationServices.FusedLocationApi;

    public FusedLocationService(Service locationService, final long INTERVAL, final long FASTEST_INTERVAL) {
        Logger.log(TAG, "FusedLocationService called");

        this.mCallBack = (OnLocationChangedListener)locationService;

        locationRequest = LocationRequest.create();
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        locationRequest.setInterval(INTERVAL);
        locationRequest.setFastestInterval(FASTEST_INTERVAL);
        this.locationService = locationService;

        googleApiClient = new GoogleApiClient.Builder(locationService)
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

        if (googleApiClient != null) {
            googleApiClient.connect();
        }
    }

    @Override
    public void onConnected(Bundle connectionHint) {
        if(mRequestingLocationUpdates) {
            startLocationUpdates();
        }else{
            Logger.log(TAG, "Location updates are already running.");
        }
    }

    protected void startLocationUpdates() {
        this.fusedLocationProviderApi.requestLocationUpdates(
                googleApiClient, locationRequest, this);
        this.mRequestingLocationUpdates = false;
    }

    @Override
    public void onLocationChanged(Location mCurrentLocation) {
        Logger.log(TAG, "onLocationChanged called");
        this.mCurrentLocation = mCurrentLocation;
        this.mCallBack.OnLocationChanged(this.mCurrentLocation);
    }

    public void startLocationUpdatesAfterResume(){
        if (googleApiClient.isConnected() && !mRequestingLocationUpdates) {
            Logger.log(TAG, "startLocationUpdatesAfterResume called");
            this.startLocationUpdates();
        }
    }

    public void stopLocationUpdates() {
        Logger.log(TAG, "stopping Location Updates");
        LocationServices.FusedLocationApi.removeLocationUpdates(
                googleApiClient, this);
    }

    public Location getLocation() {
        return this.mCurrentLocation;
    }

    @Override
    public void onConnectionSuspended(int i) {
        this.mRequestingLocationUpdates = true;
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        this.mRequestingLocationUpdates = true;
    }
}
like image 886
Georgi Angelov Avatar asked Feb 13 '15 03:02

Georgi Angelov


People also ask

How accurate is fused location provider?

The accuracy values are typically more than 10 meters, while a third party GPS app (such as GPS test) typically reaches better accuracy if I wait long enough.

How does a fused location provider work?

The fused location provider manages the underlying location technologies, such as GPS and Wi-Fi, and provides a simple API that you can use to specify the required quality of service. For example, you can request the most accurate data available, or the best accuracy possible with no additional power consumption.

How do I turn off fused location?

In the app select the System tab search for the service named Fused Location and disable it. Also search for the service group Google Play Services. Inside it there will be another Fused Location service.

Is fused location API free?

It is free of cost. If you just want to get GPS location, it is free. If you use other APIs from Google Play Service then you might come under quota limit.


1 Answers

I had the same issue like you, which is not an issue at all. Android used to have a GPS button that let you control it directly, but they replaced it with a Location button which works different. In order to get any type of location, you must turn it on. Like you, I thought the Location button turns on and off the GPS only, but that's not the case. You can control the GPS by changing the location mode: 1. High accuracy (GPS, Wi-Fi and mobile networks) 2. Power Saving (Wi-Fi and mobile networks) 3. GPS only

like image 79
Liav Avatar answered Oct 20 '22 06:10

Liav