Courtesy: GitHub

Location Updates

Demonstrates how to use the Geocode API and reverse geocoding to display a device's location as an address.

Introduction

This sample builds on the BasicLocationSample and the LocationUdpates samples included in this repo. Those samples work with latitude and longitude values only. While latitude and longitude are useful for calculating distance or displaying a map position, in many cases the address of the location is more useful. The Android framework location APIs provide a Geocode API which contains a [getFromLocation()](http://developer.android.com/reference/android/location/Geocoder.html#getFromLocation(double, double, int)) method that returns an estimated street address corresponding to a given latitude and longitude. This sample uses the getFromLocation() method to do location address lookup, an IntentService to fetch the location address, and a ResultReceiver to process results sent by the IntentService.

To run this sample, location must be enabled.

Prerequisites

  • Android API Level >v9
  • Android Build Tools >v21
  • Google Support Repository

Getting Started

This sample uses the Gradle build system. To build this project, use the "gradlew build" command or use "Import Project" in Android Studio.

Support

If you've found an error in this sample, please file an issue: https://github.com/googlesamples/android-play-location/issues

Patches are encouraged, and may be submitted according to the instructions in CONTRIBUTING.md.

License

Copyright 2014 Google, Inc.

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

compileSdkVersion 25

applicationId "com.google.android.gms.location.sample.locationaddress"

minSdkVersion 16

targetSdkVersion 25

versionCode 1

versionName "1.0"

compile fileTree(dir: 'libs', include: ['*.jar'])

compile 'com.android.support:appcompat-v7:25.3.1'

testCompile 'junit:junit:4.12'

compile 'com.android.support:design:25.3.1'

compile 'com.google.android.gms:play-services-location:11.0.0'

package com.google.android.gms.location.sample.locationaddress

uses-permission

  • android.permission.ACCESS_FINE_LOCATION

MainActivity

Getting the Location Address. Demonstrates how to use the {@link android.location.Geocoder} API and reverse geocoding to display a device's location as an address. Uses an IntentService to fetch the location address, and a ResultReceiver to process results sent by the IntentService. Android has two location request settings: {@code ACCESS_COARSE_LOCATION} and {@code ACCESS_FINE_LOCATION}. These settings control the accuracy of the current location. This sample uses ACCESS_FINE_LOCATION, as defined in the AndroidManifest.xml. For a starter example that displays the last known location of a device using a longitude and latitude, see https://github.com/googlesamples/android-play-location/tree/master/BasicLocation. For an example that shows location updates using the Fused Location Provider API, see https://github.com/googlesamples/android-play-location/tree/master/LocationUpdates.

Provides access to the Fused Location Provider API.

Represents a geographical location.

Tracks whether the user has requested an address. Becomes true when the user requests an address and false when the address (or an error message) is delivered.

The formatted location address.

Receiver registered with this activity to get the response from FetchAddressIntentService.

Displays the location address.

Visible while the address is being fetched.

Kicks off the request to fetch an address when pressed.

Updates fields based on data stored in the bundle.

Runs when user clicks the Fetch Address button.

Creates an intent, adds location data to it as an extra, and starts the intent service for fetching an address.

Gets the address for the last known location.

Updates the address in the UI.

Toggles the visibility of the progress bar. Enables or disables the Fetch Address button.

Shows a toast with the given text.

Receiver for data sent from FetchAddressIntentService.

Receives data sent from FetchAddressIntentService and updates the UI in MainActivity.

Shows a {@link Snackbar} using {@code text}. * @param text The Snackbar text.

Shows a {@link Snackbar}. * @param mainTextStringId The id for the string resource for the Snackbar text. @param actionStringId The text of the action item. @param listener The listener associated with the Snackbar action.

Return the current state of the permissions needed.

Callback received when a permissions request has been completed.

FetchAddressIntentService

Asynchronously handles an intent using a worker thread. Receives a ResultReceiver object and a location through an intent. Tries to fetch the address for the location using a Geocoder, and sends the result to the ResultReceiver.

The receiver where results are forwarded from this service.

This constructor is required, and calls the super IntentService(String) constructor with the name for a worker thread.

Tries to get the location address using a Geocoder. If successful, sends an address to a result receiver. If unsuccessful, sends an error message instead. Note: We define a {@link android.os.ResultReceiver} inMainActivity to process content sent from this service. * This service calls this method from the default worker thread with the intent that started the service. When this method returns, the service automatically stops.

Sends a resultCode and message to the receiver.