12

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).

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
TomDT
  • 330
  • 1
  • 2
  • 15
  • Its using a high accuracy request. THis means it needs GPS. Are you sure you have GPS locked? If you're debugging you're probably indoors, and that tends not to work. Is the GPS icon not on at all, on but flashing, or on and solid? – Gabe Sechan Jul 30 '14 at 22:23
  • :'( Yep, actually it's tilting... Although, apparently it was also that my wifi connection wasn't "real" because there was an authentication process which I hadn't notice, so basically I wasn't connected (Access to websites but maybe not to other features?). I just tried it with two devices and got the updates normally (authenticated) and not in the other one... And yes, I'm indoors so maybe that's why I'm getting the tilting GPS but as I've read the HIGH_ACCURACY doesn't require it (but it's most likely to) - http://preview.tinyurl.com/k6ew5uv Anyway, thank you ! – TomDT Jul 31 '14 at 03:53
  • From that link: "PRIORITY_HIGH_ACCURACY is more likely to use GPS" so yes, it uses GPS. It may not in some circumstances, but it definitely does in others. – Gabe Sechan Jul 31 '14 at 03:58
  • Ah yeah, I was just thinking of why it still worked with only wifi :) – TomDT Jul 31 '14 at 15:05

2 Answers2

1

The three callbacks:

public void onConnectionFailed(ConnectionResult arg0){}
public void onConnected(Bundle bundle){}
public void onConnectionSuspended(int arg0){}

must be implemented. However, I don't see onConnectionFailed being overridden. The compiler should have given an error in this case. There are two LocationListener implementations one for LocationListener and the other gms.location.LocationListener. You may want to check if you are using the gms version.

I hope Google will update their training sample code to the new client model and eliminate further confusion.

camelCaseCoder
  • 1,447
  • 19
  • 32
GregM
  • 750
  • 7
  • 13
1

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).

TomDT
  • 330
  • 1
  • 2
  • 15
  • I made a similar mistake to you - i did not switch on the GPS on my emulator. Its so easy to forget to switch the GPS on there and Genymotion seems to show the location icon on the notification bar even if the GPS is not switched on in the emulator. – Simon May 24 '15 at 18:51
  • Hi, can you please tell me what is wrong with your code above? I also have the same problem here. My Wifi is on, as well as my GPS. Thanks – Dark Leonhart Feb 22 '16 at 07:03