Android Location Basics


Sample app that will demonstrate the basics in retrieving a user’s location and requesting permission at runtime on Android.

Sample Android App GitHub Repository

Setup Google Play Services In Android Studio

If you open up Android Studio Settings, go to File -> Settings -> Appearance & Behavior -> System Settings -> Android SDK. Then click on the SDK Tools tab and check Google Play services

Setup Google Map Fragment

Add the dependency to the application’s build.gradle file:

dependencies {

    implementation ''


Get an API key for maps by visiting this site and creating a project. Once you have the key, for debugging purposes we will add this key to a string resource file in the debug folder.

 <!--  app/src/debug/res/values/google_maps_api.xml-->
    <string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">YOUR_KEY_HERE</string>

Now in the AndroidManifest.xml, place the following inside the application tag.


        android:value="@string/google_maps_key" />


Now create a SupportMapFragment by creating a fragment tag in an xml layout file.

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android=""
    tools:context=".MapsActivity" />

Now run the app, and the map should load up!

Doing More with the SupportMapFragment

In your fragment or activity, use the FragmentManager to find the fragment. and call SupportMapFragment.getMapAsync {} to perform actions once the map itself is ready.

 val mapFragment = supportFragmentManager.findFragmentById( as SupportMapFragment
        mapFragment.getMapAsync { googleMap: GoogleMap? ->
            googleMap ?: return@getMapAsync
            val paddingBottom =
                TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 60f, resources.displayMetrics)
            googleMap.apply {
                setPadding(0,0,0, paddingBottom)
                // Add a marker in Sydney and move the camera
                val sydney = LatLng(-34.0, 151.0)
                addMarker(MarkerOptions().position(sydney).title("Marker in Sydney"))

This sets a padding on the bottom so the Google Logo and buttons will render above the FAB. Then it creates a marker for Sydney Australia and pans to the camera to this location for the user.