7.1 Location
7.1 Location
Location
Lesson 7
Tower Bridge,
Tower Bridge Rd,
London SE1 2UP
UK
This work is licensed under a Creative
Advanced Android Development Location Commons Attribution 4.0 International 8
License.
Setting up
Google Play
services
compile 'com.google.android.gms:play-services:xx.x.x'
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case REQUEST_LOCATION_PERMISSION: {
// Check if the permission is granted.
This work is licensed under a Creative
Advanced Android Development Location Commons Attribution 4.0 International 20
License.
Check if request was granted
● Response is returned in permissions array
● Compare grantResults parameter to
PackageManager.PERMISSION_GRANTED
// Check if the permission is granted.
if (grantResults.length > 0) && grantResults[0] ==
PackageManager.PERMISSION_GRANTED) {
// Permission was granted.
...
} else {// Permission was denied...
This work is licensed under a Creative
Advanced Android Development Location Commons Attribution 4.0 International 21
License.
Get device
location
● Use FusedLocationProviderClient
to request last known location
● Usually, last known location is same
as current location
FusedLocationProviderClient flpClient =
LocationServices.getFusedLocationProviderClient(
context);
getFromLocation(
double latitude, double longitude, int maxResults)
LocationSettingsRequest settingsRequest =
new LocationSettingsRequest.Builder()
SettingsClient client =
LocationServices.getSettingsClient(this);