Chapter 7
Chapter 7
Location, location, location. The changing location of the user and the device used by the user
make mobile applications fundamentally different from their stationary counterparts. Yet, most
software developers, even those who have some experience with mobile applications, have
little experience and understanding in how location-based information is gathered and
distributed and how this information may be utilized by mobile applications. Although we will
not be able to cover all aspects of location-based information in mobile applications, we will
try to tackle the basic problems in this chapter.
If you have looked into developing mobile applications, you have certainly heard of “location-
based services.” The UTMS Forum defines location-based services as follows:
Specifically, use the fused location provider to retrieve the device’s last known location. The
fused location provider is one of the location APIs in Google Play services. It manages the
underlying location technology and provides a simple API so that you can specify
requirements at a high level, like high accuracy or low power. It also optimizes the device’s
use of battery power.
In your activity’s onCreate() method, create an instance of the Fused Location Provider
Client as the following code snippet shows.
Java
@Override
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
The getLastLocation() method returns a Task that you can use to get a Location object with
the latitude and longitude coordinates of a geographic location. The location object may
be null in the following situations:
Location is turned off in the device settings. The result could be null even if the last location
was previously retrieved because disabling location also clears the cache.
The device never recorded its location, which could be the case of a new device or a device
that has been restored to factory settings.
Google Play services on the device has restarted, and there is no active Fused Location
Provider client that has requested location after the services restarted. To avoid this situation,
you can create a new client and request location updates yourself. For more information,
see Receiving Location Updates.
Google Location Accuracy for your Android device (a.k.a. Google Location
Services): To get a more accurate location for your phone, learn how to manage Location
Accuracy.
Emergency Location Service for your Android device: Learn how to manage Android
Emergency Location Service.
Earthquake alerts for your Android device: To get updates for nearby earthquakes on your
phone, learn how to manage Earthquake alerts.
Use location for time zone on your Android device: To get time zone updates based on
location, learn how to manage location for time zone.
Location History for your Google Account: To find and manage the places your phone has
been, learn how to turn on Location History.
Location Sharing for Google Maps: To let others know where your phone is, learn how to
share your real-time location via Google Maps.
Location in Search: To get more helpful results when you search on Google, learn how to
manage location permissions for websites and apps.
Wi-Fi scanning and Bluetooth scanning: To help apps get better location info, learn how to
scan for network or bluetooth devices.