Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Documentation for new Android Location API

Options
  • 30-07-2014 11:44am
    #1
    Registered Users Posts: 6,464 ✭✭✭


    Maybe (probably?) I'm being a bit thick, but I can't seem to find any sample documentation for the latest version of the Android location API, under Google Play Services 4.2.

    There's some blurb on it here, which focuses mainly on Plus and Drive, but as far as I can see this also applies to Location.

    The android training docs use the GooglePlayServicesClient API, along with LocationClient.

    These are all marked as deprecated in the API references, in favour of GoogleApiClient, and LocationServices.

    There's a FusedLocationProviderApi and ActivityRecognitionApi, which defines a load of abstract methods, which I presume something else implements, but I can't work out what that is.

    All the high level documentation seems to be pushing the new API, but all the examples are using the older version. Most of the examples I can find on StackOverflow, etc are using the GooglePlayServicesClient version - most of those seem to derived from the (outdated) official docs anyway.

    Does anyone have a link to an example of using the new API for location tracking and activity recognition?


Comments

  • Registered Users Posts: 1,091 ✭✭✭brian plank


    if you follow the steps here you should get something working. It covers how to find your location, activity recognition, geofences etc.


  • Registered Users Posts: 6,464 ✭✭✭MOH


    if you follow the steps here you should get something working. It covers how to find your location, activity recognition, geofences etc.

    They're using the deprecated API calls. They work alright, but I figure if I'm learning how to do something I may as well use the most up-to-date API.

    I did eventually hack together something with the new API, I'll post details when I get a chance.


  • Registered Users Posts: 6,464 ✭✭✭MOH


    Sorry, kept meaning to post this, this is what I ended up with.

    Doing it the "old" way, you'd end up with something like this:
    LocationManager  locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    String provider = locationManager.getBestProvider(criteria, false);
    locationManager.requestLocationUpdates(provider, updateInterval, 0, this);		
    
    ....
    public void onLocationChanged(Location location) { .....
    //do whatever you want with the location update
    

    Whereas using the "new" API:
    GoogleApiClient mGoogleApiClient;
    LocationRequest mLocationRequest;
    ...
    mGoogleApiClient = new GoogleApiClient.Builder(this)		
    .addApi(LocationServices.API)
    .addConnectionCallbacks((GoogleApiClient.ConnectionCallbacks) this)
    .addOnConnectionFailedListener(this)
    .build();
    
    ....
    public void onConnected(Bundle arg0) {
    mLocationRequest = LocationRequest.create(); 
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setInterval(UPDATE_INTERVAL);
    mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
    }
    
    ...
    public void onLocationChanged(Location location) { .....
    //do whatever you want with the location update
    

    You'll need to import:
    com.google.android.gms.common.api.GoogleApiClient;
    com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
    com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener
    (note it's the GoogleApiClient, instead of the GooglePlayServicesClient version).

    As well as LocationListener, your class also needs to implement ConnectionCallbacks and OnConectionFailedListener (assuming the same class is handling all the callbacks).

    Also, onDisconnected() has been renamed to onConnectionSuspended(int cause).


    I'm still mucking around with this but it seems to be working, hopefully might help someone else work it out.


Advertisement