Difference between revisions of "MAP524/DPS924 Lecture 9"

From CDOT Wiki
Jump to: navigation, search
(Setup to use Google Play Services)
(Setup to use Google Play Services)
Line 46: Line 46:
 
* See [https://developers.google.com/android/guides/setup here]. Note it's talking about the build.gradle file inside the app subdirectory of your project.
 
* See [https://developers.google.com/android/guides/setup 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.
 
* 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 emulator.
+
* 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 [https://www.genymotion.com GenyMotion emulator] and follow unofficial [https://github.com/codepath/android_guides/wiki/Genymotion-2.0-Emulators-with-Google-Play-support setup instructions] to get google maps to work in that emulator.
  
 
== Using Google Maps ==
 
== Using Google Maps ==

Revision as of 06:18, 5 August 2015

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:

Emulator-control-panel.png

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

Using Google Maps

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.
  • Add that key to your app.
  • If you haven't already done it - follow the "Setup to use Google Play Services" section above.