Open main menu

CDOT Wiki β

MAP524/DPS924 Lecture 9

Revision as of 04:50, 5 August 2015 by Andrew (talk | contribs) (Getting your location)

Location APIs

There are two: android.location and the Google Play Services API. They both work and Google encourages you to use their proprietary APIs, which may but may not be in fact in your interest.

We'll use the android.location API: it requires very little setup and there's a very nice tutorial available from Vogella.

Setup to use location

  • If you forget to ask for permissions to read the location, you'll just get a null provider without any error or warning messages. So add the appropriate permission to your manifest.

Getting your location

  • You don't read the location directly from the GPS, you get it from the Location Manager - a system service. You'll want to keep the reference to this service around:
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  • We'll need to keep the name of the provider we want to use around. This code asks for the best provider given the criteria and the permissions in our manifest:
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
  • Now you're ready to ask to receive location updates from the location manager. Easiest way to do that is via a callback, so we'll make our activity implement LocationListener and add this code to start/stop getting updates:
locationManager.requestLocationUpdates(provider, 400, 1, this);
// ...
locationManager.removeUpdates(this);
  • The code above should live in onResume()/onPause() (remember the activity lifecycle) because getting the location uses significant battery power and your app will continue to get location even if it's not in the foreground (e.g. if the lock screen is on).
  • The required LocationListener method onLocationChanged() will be called when the location is updated.
  • On an emulator you won't get any location updates automatically, you'll need to set them using telnet:
$ telnet localhost 5554
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Android Console: type 'help' for a list of commands
OK
geo fix 13.24 52.31
OK
  • Or the Emulator Control panel in Android Device Monitor:

 

Setup to use Google Play Services

Using the Google Play Location API is a challenge - they try to make it easier but at the same time using this special API makes things harder. If you really want to use it, these are the steps you need to follow:

  • Go to your SDK Manager and make sure the latest versions of "Google Play services" and "Google Repository" are installed. When you do that - make sure to not install any other updates. The SDK Manager likes to suggest all kinds of updates that you don't need and will use up all your disk space.
  • See here. Note it's talking about the build.gradle file inside the app subdirectory of your project.
  • I didn't manage to get this to work on any version of the emulator, but it did work on my Nexus 5.