Reader small image

You're reading from  How to Build Android Apps with Kotlin - Second Edition

Product typeBook
Published inMay 2023
PublisherPackt
ISBN-139781837634934
Edition2nd Edition
Right arrow
Authors (4):
Alex Forrester
Alex Forrester
author image
Alex Forrester

Alex Forrester is an experienced software developer with more than 20 years of experience in mobile, web development, and content management systems. He has been working with Android for over 8 years, creating flagship apps for blue-chip companies across a broad range of industries at Sky, The Automobile Association, HSBC, The Discovery Channel, and O2. Alex lives in Hertfordshire with his wife and daughter. When he's not developing, he likes rugby and running in the Chiltern hills.
Read more about Alex Forrester

Eran Boudjnah
Eran Boudjnah
author image
Eran Boudjnah

Eran Boudjnah is a developer with over 20 years of experience in developing desktop applications, websites, interactive attractions, and mobile applications. He has been working with Android for about 7 years, developing apps and leading mobile teams for a wide range of clients, from start-ups (JustEat) to large-scale companies (Sky) and conglomerates. He is passionate about board games (with a modest collection of a few hundred games) and has a Transformers collection he's quite proud of. Eran lives in North London with Lea, his wife.
Read more about Eran Boudjnah

Alexandru Dumbravan
Alexandru Dumbravan
author image
Alexandru Dumbravan

Alexandru Dumbravan has been an Android Developer since 2011 and worked across a variety of Android applications which contained features such as messaging, voice calls, file management, and location. He continues to broaden his development skills while working in London for a popular fintech company.
Read more about Alexandru Dumbravan

Jomar Tigcal
Jomar Tigcal
author image
Jomar Tigcal

Jomar Tigcal is an Android developer with over 10 years of experience in mobile and software development. He worked on various stages of app development for small startups to large companies. Jomar has also given talks and conducted training and workshops on Android. In his free time, he likes running and reading. He lives in Vancouver, Canada with his wife Celine.
Read more about Jomar Tigcal

View More author details
Right arrow

Android Permissions and Google Maps

This chapter will teach you how to request and obtain app permissions in Android. You will gain a solid understanding of how to include local and global interactive maps in your app by using the Google Maps API and how to request permissions to use device features that provide richer functionality.

By the end of the chapter, you will be able to create permission requests for your app and handle missing permissions.

In the previous chapter, we learned how to present data in lists using RecyclerView. Then, we used that knowledge to present the user with a list of secret cat agents. In this chapter, we will learn how to find the user’s location on the map and how to deploy cat agents to the field by selecting locations on the map.

First, we will explore the Android permissions system. Many Android features are not immediately available to us. These features are gated behind a permission system to protect the user. For us to access those...

Technical requirements

The complete code for all the exercises and the activity in this chapter is available on GitHub at https://packt.link/6ShZd

Requesting permission from the user

Our app might want to implement certain features that Google deems dangerous. This usually means access to those features could risk the user’s privacy. For example, some permissions may allow you to read users’ messages or determine their current location.

Depending on the required permission and the target Android API level we are developing, we may need to request that permission from the user. If the device is running on Android 6 (Marshmallow, API level 23), and the target API of our app is 23 or higher (it almost certainly will be, as most devices by now will run newer versions of Android), there will be no alert for the user about any permissions requested by the app at install time. Instead, our app must ask the user to grant those permissions at runtime.

When we request permission, the user sees a dialog like the one shown in Figure 7.1.

Figure 7.1 – Permission dialog for device location access

Figure 7.1 – Permission dialog for device location...

Showing a map of the user’s location

Having successfully obtained permission from the user to access their location, we can now ask the user’s device to provide us with its last known location. This is also usually the user’s current location. We will then use this location to present the user with a map of their current location.

To obtain the user’s last known location, Google has provided us with the Google Play Location service and, more specifically, with the FusedLocationProviderClient class. The FusedLocationProviderClient class helps us interact with Google’s Fused Location Provider API, which is a location API that intelligently combines different signals from multiple device sensors to provide us with device location information.

To access the FusedLocationProviderClient class, we must first include the Google Play Location service library in our project. This simply means adding the following code snippet to the dependencies block...

Map clicks and custom markers

With a map showing the user’s current location by zooming in at the right location and placing a marker there, we have a rudimentary knowledge of how to render the desired map and how to obtain the required permissions and the user’s current location.

In this section, we will learn how to respond to a user interacting with the map and how to use markers more extensively. We will learn how to move markers on the map and replace the default pin marker with custom icons. When we know how to let the user place a marker anywhere on the map, we can let them choose where to deploy the secret cat agent.

We need to add a listener to the GoogleMap instance to listen for clicks on the map. Looking at our MapsActivity.kt file, the best place to do so would be in onMapReady(GoogleMap). A naïve implementation might look like this:

override fun onMapReady(googleMap: GoogleMap) {
    mMap = googleMap.apply {
  ...

Summary

In this chapter, we learned about Android permissions. We touched on the reasons for having them and saw how we could request the user’s permission to perform certain tasks. We also learned how to use Google’s Maps API and how to present the user with an interactive map.

Lastly, we leveraged our knowledge of presenting a map and requesting permissions to find out the user’s current location and present it on the map. Of course, there is a lot more that can be done with the Google Maps API, and you could explore a lot more possibilities with certain permissions.

You should now have enough understanding of the foundations to explore further. To read more about permissions, visit https://packt.link/57BdN. To read more about the Maps API, visit https://packt.link/8akrP.

In the next chapter, we will learn how to perform background tasks using Services and WorkManager. We will also learn how to present the user with notifications, even when the app is...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
How to Build Android Apps with Kotlin - Second Edition
Published in: May 2023Publisher: PacktISBN-13: 9781837634934
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 $15.99/month. Cancel anytime

Authors (4)

author image
Alex Forrester

Alex Forrester is an experienced software developer with more than 20 years of experience in mobile, web development, and content management systems. He has been working with Android for over 8 years, creating flagship apps for blue-chip companies across a broad range of industries at Sky, The Automobile Association, HSBC, The Discovery Channel, and O2. Alex lives in Hertfordshire with his wife and daughter. When he's not developing, he likes rugby and running in the Chiltern hills.
Read more about Alex Forrester

author image
Eran Boudjnah

Eran Boudjnah is a developer with over 20 years of experience in developing desktop applications, websites, interactive attractions, and mobile applications. He has been working with Android for about 7 years, developing apps and leading mobile teams for a wide range of clients, from start-ups (JustEat) to large-scale companies (Sky) and conglomerates. He is passionate about board games (with a modest collection of a few hundred games) and has a Transformers collection he's quite proud of. Eran lives in North London with Lea, his wife.
Read more about Eran Boudjnah

author image
Alexandru Dumbravan

Alexandru Dumbravan has been an Android Developer since 2011 and worked across a variety of Android applications which contained features such as messaging, voice calls, file management, and location. He continues to broaden his development skills while working in London for a popular fintech company.
Read more about Alexandru Dumbravan

author image
Jomar Tigcal

Jomar Tigcal is an Android developer with over 10 years of experience in mobile and software development. He worked on various stages of app development for small startups to large companies. Jomar has also given talks and conducted training and workshops on Android. In his free time, he likes running and reading. He lives in Vancouver, Canada with his wife Celine.
Read more about Jomar Tigcal