Reader small image

You're reading from  Android 9 Development Cookbook - Third Edition

Product typeBook
Published inOct 2018
Reading LevelIntermediate
PublisherPackt
ISBN-139781788991216
Edition3rd Edition
Languages
Tools
Right arrow
Author (1)
Rick Boyer
Rick Boyer
author image
Rick Boyer

Rick Boyer has been programming professionally for over 20 years. He has written apps on Windows, created websites, and coded for various mobile devices, including Windows CE, Windows Phone, and Android. Almost eight years ago, he took the plunge and started his own software consulting business, NightSky Development, focusing exclusively on Android development.
Read more about Rick Boyer

Right arrow

Chapter 14. Location and Using Geofencing

In this chapter, we will cover the following topics:

  • How to get the device location
  • Resolving problems reported with the GoogleApiClientOnConnectionFailedListener
  • Creating and monitoring a Geofence

Introduction


Location awareness offers many benefits to an app, so many in fact that even desktop apps now attempt to get the user's location. Location uses ranges from turn-by-turn directions, "find the nearest" applications, alerts based on location, and there are now even location-based games that get you out exploring with your device.

The Google APIs offer many rich features for creating location-aware applications and mapping features. Our first recipe will look at obtaining the last known location on the device along with receiving updates as the location changes. If you are requesting location updates for a proximity location, take a look at using the Geofence option instead in the Create and monitor a Geofence recipe.

All the recipes in this chapter use the Google Libraries. If you have not already downloaded the SDK Packages, follow the instructions from Google.

Now that you have the location...

How to get the device location


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.

Getting ready

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.

How to do it...

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:

  1. Add the following permission:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
  1. Under the Gradle Scripts section, open the build.gradle (Module: app) file, as shown in this screenshot:
  1. Add the following statement to the dependencies...

Resolving problems reported with the GoogleApiClient OnConnectionFailedListener


With the constantly changing nature of Google APIs, your users are likely to attempt to use your application, but not be able to because their files are out of date. We can use the GoogleApiAvailability library to display a dialog to help the user resolve the problem.

We'll continue with the previous recipe and add code to the onConnectionFailed() callback. We'll use the error result to display additional information to the user to resolve their problem.

Getting ready

Create a new project in Android Studio and call it HandleGoogleAPIError. Use the default Phone & Tablet options, and select Empty Activity when prompted for Activity Type. Once you've created the project, add the Google Play library reference to the project dependencies. (See the previous recipe steps.)

How to do it...

The first step for this recipe is to add the Google Play Services library to the project. From there, we'll create the classes to...

Creating and monitoring a Geofence


If your application needs to know when the user enters or exits a certain location, there's an alternative to continuously checking the user location: Geofencing. A Geofence is a location (latitude and longitude) along with a radius. You can create a Geofence and let the system notify you when the user enters the location proximity you specified. (Android currently allows up to 100 Geofences per user.)

Geofence properties include:

  • Location: The longitude and latitude
  • Radius: The size of the circle (in meters)
  • Loitering delay: How long the user may remain within the radius before sending notifications
  • Expiration: How long until the Geofence automatically expires
  • Transitiontype:
    • GEOFENCE_TRANSITION_ENTER
    • GEOFENCE_TRANSITION_EXIT
    • INITIAL_TRIGGER_DWELL

This recipe will show you how to create a Geofence object and use it to create an instance of GeofencingRequest.

Getting ready

Create a new project in Android Studio and call it Geofence. Use the default Phone & Tablet...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Android 9 Development Cookbook - Third Edition
Published in: Oct 2018Publisher: PacktISBN-13: 9781788991216
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £13.99/month. Cancel anytime

Author (1)

author image
Rick Boyer

Rick Boyer has been programming professionally for over 20 years. He has written apps on Windows, created websites, and coded for various mobile devices, including Windows CE, Windows Phone, and Android. Almost eight years ago, he took the plunge and started his own software consulting business, NightSky Development, focusing exclusively on Android development.
Read more about Rick Boyer