MAP524/DPS924 Lecture 9
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:
- And you can use a service like findlatitudeandlongitude.com to get some meaningful numbers for testing.
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.
- You'll need an emulator with the proprietary Google Play libraries installed. Your existing emulator might not have them.
- For Google Maps you'll need a real device, the maps won't work on an Android emulator (with or without Google services on the image).
- You can use the free GenyMotion emulator and follow unofficial setup instructions to get google maps to work in that emulator.
- To run GenyMotion on the ICT_USB_Sticks you'll need to install VirtualBox like so (as root):
apt-get install virtualbox-4.3
- To run GenyMotion on the ICT_USB_Sticks you'll need to install VirtualBox like so (as root):
- You can use the free GenyMotion emulator and follow unofficial setup instructions to get google maps to work in that emulator.
Using Google Maps
Obtain API Access
There are many guides here.
You can use for low-volume purposes for free. Google will track your usage of the maps API using an API key - without a key you can't use the service in your app.
- Use "keytool" command to make/view your key store.
keytool -list -v -keystore /home/$USER/.android/debug.keystore
- You can use the output to get a key associated with your application (which you want to do in production). That doesn't seem to be required any more for development purposes.
- Obtain a Maps API key from the Google APIs Console (which is not the same as your Google Play developer console) by providing your application's signing certificate and its package name. You'll obviously need a Google account.
- Create a new project (you won't have a choice).
- Enable the Google Maps Android API v2 under APIs.
- In Credentials/Public API access add a new Android key.
- I had to wait a minute for it to show up.
Code
- If you haven't already done it - follow the "Setup to use Google Play Services" section above.
- Add the API key to your app.
- The rest of the code you can find in parts of this tutorial, specifically to get the minimum going you'll need to:
- Add the appropriate fragment XML code to your activity layout and position it so that it doesn't cover other elements in your activity.
- Make your activity implement FragmentActivity.
- Use getMapAsync() to set up your activity to be the callback for when the map loads.
- Do something interesting in onMapReady()