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