Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using GoogleApiClient + LocationServices not updating

I'm just trying to do a simple 'tutorial' app to get my phone's location (to learn how to use it later in some other app) but I'm just not getting anywhere.

What I've done

  1. Android Developer's tutorial : First of, I followed the tutorial in the Android's developer site (developer.android.com/training/location/receive-location-updates.html).

    Just like indicated in there, I used a Location, LocationClient and LocationRequest, initializing them (and setting them up) in onCreate. LocationClient is connected and disconnected in onStart and onStop.

    I'm requesting location update after I'm connected (in onConnected). I verify that GooglePlayServices are available before making this call and I'm still not getting any update in "onLocationChanged".

  2. GoogleApiClient : I noticed that LocationClient is deprecated and LocationServices are preferred (developer.android.com/reference/com/google/android/gms/location/LocationClient.html).

    As indicated here : https://developer.android.com/reference/com/google/android/gms/location/FusedLocationProviderApi.html, I use a GoogleApiClient and set it to LocationServices (also https://stackoverflow.com/a/25013850/3802589). So then I set it all to use this but still I'm not getting any updates on the location.

onLocationChanged it should print if there's a response. I also put a button that prints location or 'nothing'.

Remarks

I checked using Google Maps to see if maybe I had something off but it's working just fine.

Project

build.gradle

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.google.android.gms:play-services:5.0.77'
    compile "com.android.support:support-v4:20.0.+"
}

Manifest

...
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
...
<meta-data android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
...

Code

public class MyActivity extends Activity  implements
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,
        LocationListener {

    private GoogleApiClient mLocationClient;
    private Location mCurrentLocation;
    LocationRequest mLocationRequest;

    ...
    /* Constant fields - request interval */
    ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        mLocationClient = new GoogleApiClient.Builder(getApplicationContext())
                                .addApi(LocationServices.API)
                                .addConnectionCallbacks(this)
                                .addOnConnectionFailedListener(this)
                                 .build();

        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(UPDATE_INTERVAL);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

        mLocationRequest.setFastestInterval(FASTEST_INTERVAL);

    }

    @Override
    protected void onStart() {
        super.onStart();
        mLocationClient.connect();
    }

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

        LocationServices.FusedLocationApi.removeLocationUpdates(mLocationClient, this);

        mLocationClient.disconnect();
    }

    public void onClick(View view) {

        if (mCurrentLocation != null) {
            Log.i("Location", mCurrentLocation.toString());
        } else {
            Log.i("Location", "nothing");
        }

    }

    @Override
    public void onLocationChanged(Location location) {
        Log.d("Location Update", "CHANGED");
        mCurrentLocation = location;
    }

    @Override
    public void onConnected(Bundle bundle) {
        // Display the connection status
        Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show();
        if(servicesConnected()) {
            LocationServices.FusedLocationApi.requestLocationUpdates(mLocationClient, mLocationRequest, this);
        }
    }

    @Override
    public void onConnectionSuspended(int i) {
        Toast.makeText(this, "Disconnected. Please re-connect.",
                Toast.LENGTH_SHORT).show();
    }

logcat

8948-8948/example.android.com.test D/Location Updates﹕ Google Play services is available.
8948-8948/example.android.com.test I/Location﹕ nothing

I feel that I might be forgetting something really simple... but I just can't see it and I've spent too much time trying to get just a simple location...

I could maybe just use the android.location API but... It bothers me that this is so hard (supposedly being easier).

like image 501
TomDT Avatar asked Jul 30 '14 22:07

TomDT


People also ask

How to get current location on android?

There are two ways to get the current location of any Android device: Android's Location Manager API. Fused Location Provider: Google Play Services Location APIs.

What does fused location mean?

The fused location provider is a location API in Google Play services that intelligently combines different signals to provide the location information that your app needs.


1 Answers

The mistake was only that wifi was not on... And I didn't realise.

As to what GregM said, if I'm correct that's the old API (most of it deprecated right now) and as stated in the developer's site (https://developer.android.com/google/auth/api-client.html) you have to use a GoogleApiClient as shown in my original question (and same for the callbacks).

like image 193
TomDT Avatar answered Nov 02 '22 00:11

TomDT