You're reading from Android 9 Development Cookbook - Third Edition
Location awareness offers many benefits to an app, so many in fact that even desktop apps now attempt to get the user's location. Location uses ranges from turn-by-turn directions, "find the nearest" applications, alerts based on location, and there are now even location-based games that get you out exploring with your device.
The Google APIs offer many rich features for creating location-aware applications and mapping features. Our first recipe will look at obtaining the last known location on the device along with receiving updates as the location changes. If you are requesting location updates for a proximity location, take a look at using the Geofence option instead in the Create and monitor a Geofence recipe.
All the recipes in this chapter use the Google Libraries. If you have not already downloaded the SDK Packages, follow the instructions from Google.
Note
Add SDK Packages from http://developer.android.com/sdk/installing/adding-packages.html.
Now that you have the location...
This first recipe will show you how to get the last known location. If you've worked with the Google Location APIs in the past, then you may notice things have changed. This recipe shows you the latest API for getting both the last location and updates as the location changes.
Create a new project in Android Studio and call it GetLocation
. Use the default Phone & Tablet
options, and select Empty Activity
when prompted for Activity Type
.
First, we'll add the necessary permissions to the Android Manifest, then we'll modify the TextView
element to include an ID. Finally, we'll add a method to receive the last known location callback. Open the Android Manifest and follow these steps:
- Add the following permission:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
- Under the
Gradle Scripts
section, open thebuild.gradle (Module: app)
file, as shown in this screenshot:
- Add the following statement to the
dependencies...
With the constantly changing nature of Google APIs, your users are likely to attempt to use your application, but not be able to because their files are out of date. We can use the GoogleApiAvailability
library to display a dialog to help the user resolve the problem.
We'll continue with the previous recipe and add code to the onConnectionFailed()
callback. We'll use the error result to display additional information to the user to resolve their problem.
Create a new project in Android Studio and call it HandleGoogleAPIError
. Use the default Phone & Tablet
options, and select Empty Activity
when prompted for Activity Type
. Once you've created the project, add the Google Play library reference to the project dependencies. (See the previous recipe steps.)
If your application needs to know when the user enters or exits a certain location, there's an alternative to continuously checking the user location: Geofencing. A Geofence is a location (latitude and longitude) along with a radius. You can create a Geofence and let the system notify you when the user enters the location proximity you specified. (Android currently allows up to 100 Geofences per user.)
Geofence properties include:
- Location: The longitude and latitude
- Radius: The size of the circle (in meters)
- Loitering delay: How long the user may remain within the radius before sending notifications
- Expiration: How long until the Geofence automatically expires
- Transitiontype:
GEOFENCE_TRANSITION_ENTER
GEOFENCE_TRANSITION_EXIT
INITIAL_TRIGGER_DWELL
This recipe will show you how to create a Geofence object and use it to create an instance of GeofencingRequest
.