How to get the last location
We'll start this chapter with a simple recipe that is commonly needed: how to get the last known location. This is an easy way to use APIs with very little overhead resource drain. (This means, your app won't be responsible for killing the battery.)
This recipe also provides a good introduction to setting up the Google Location APIs.
Getting ready
Create a new project in Android Studio and call it: GetLastLocation
. Use the default Phone & Tablet options, and select Empty Activity when prompted for Activity Type.
How to do it...
First, we'll add the necessary permissions to the Android Manifest, then we'll create a layout with a Button
and a TextView
element. Finally, we'll create a GoogleAPIClient
API to access the last location. 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...