In this guide
Adding a map to an Android app is one of the most useful skills a CS or IT student can learn. Delivery, campus navigation, cab booking and travel apps all start from the same building blocks: a map on screen, a marker at a place, the user's current location, and a route between two points. Master these four pieces and you can build almost any location feature your app needs.
This guide uses Kotlin and the Maps SDK for Android: set up a Cloud project, get and restrict an API key, add the SDK, show your first map, add markers, move the camera, request the location permission, and draw a route polyline.
Quick answer
Create a Google Cloud project, enable the Maps SDK for Android, and create an API key restricted to your app's package name and SHA-1 fingerprint. Add the Maps SDK dependency and your key to the project, place a map fragment in your layout, and initialise it in your activity. Once the map is ready, add markers with addMarker, move the camera with moveCamera or animateCamera, read the device location with the Fused Location Provider after requesting permission, and draw routes by decoding the encoded polyline from a Directions API response.
| What the Maps SDK gives you | What you build yourself |
|---|---|
| Map tiles, zoom, pan and gestures | Which places to show and why |
| Markers, info windows and polylines | Your own data: places, routes, orders |
| Camera movement and animation | When and where the camera should move |
The SDK renders the map; your job is the app logic around it.
What you need before starting
You need Android Studio, a Google account, and a Google Cloud project. The Maps SDK is enabled from the Cloud Console, and Google requires a billing account on the project before the SDK can be used. Usage limits and pricing change over time, so check the current Google Maps Platform pricing page before estimating any cost.
The examples use the Google Maps Activity template in Android Studio. Test on a physical device or an emulator with Google Play services; a plain image without them will not render the map.
Getting and restricting the API key
The API key identifies your Cloud project to Google and lets the Maps SDK load map tiles. Create it in the Cloud Console under APIs and Services, then enable the Maps SDK for Android for the same project; a key with no API enabled returns a blank map.
Never ship an unrestricted key. In the Cloud Console, restrict the key to Android apps and add your app's package name with the SHA-1 fingerprint of the signing certificate. Debug and release fingerprints differ, so add both; a wrong fingerprint is one of the most common reasons a map stays blank on one build but works on another. Find the debug SHA-1 with the signingReport Gradle task.
Keep the key out of version control by putting it in local.properties, which is not committed by default:
MAPS_API_KEY=YOUR_API_KEY
Then read it into your manifest through a Gradle manifest placeholder:
android {
defaultConfig {
manifestPlaceholders["MAPS_API_KEY"] =
project.properties["MAPS_API_KEY"] as String
}
}
In AndroidManifest.xml, add the key inside the application element, using the placeholder name where the real key goes:
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="YOUR_API_KEY" />
Gradle substitutes the value at build time, so the real key never appears in your source files.
Adding the SDK dependency and the map fragment
Add the Maps SDK dependency to your app-level build.gradle.kts, using the current version from the official documentation:
dependencies {
implementation("com.google.android.gms:play-services-maps:19.2.0")
implementation("com.google.android.gms:play-services-location:21.3.0")
}
The first is the Maps SDK itself; the second is the location library, which provides the Fused Location Provider used later in this guide. Sync the project, then add a map fragment to your layout:
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
You also need the internet permission, because map tiles are downloaded at runtime:
<uses-permission android:name="android.permission.INTERNET" />
Showing your first map
In your activity, get the fragment and ask to be notified when the map is ready; the map loads asynchronously, so do not call map methods until the onMapReady callback fires.
class MapsActivity : AppCompatActivity(), OnMapReadyCallback {
private lateinit var googleMap: GoogleMap
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_maps)
val mapFragment = supportFragmentManager
.findFragmentById(R.id.map) as SupportMapFragment
mapFragment.getMapAsync(this)
}
override fun onMapReady(map: GoogleMap) {
googleMap = map
}
}
Run the app: you should see a world map. If the screen is blank, check the API key, the SDK status and the key restriction; Logcat usually prints the exact error.
Adding markers and info windows
A marker pins a place on the map: create one with a LatLng position and a title, and the SDK draws the standard red pin:
val campus = LatLng(18.5204, 73.8567)
googleMap.addMarker(
MarkerOptions()
.position(campus)
.title("Main Campus")
.snippet("Entry gate, Building A")
)
The title and snippet appear in an info window when the user taps the marker, so keep both short. For several places, keep them in a list and loop over it with one addMarker call per place.
Custom icons are possible with BitmapDescriptorFactory, but one pin style is usually clearer. To react to a tap, set an OnMarkerClickListener and return false to keep the default info window.
Moving the camera
The camera decides what the user sees: the centre point and the zoom level. When your map first loads, move it to your main place so the user does not start at a world view.
val campus = LatLng(18.5204, 73.8567)
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(campus, 15f))
Zoom 15 suits a campus; 10 shows a city, 18 shows individual buildings. moveCamera jumps instantly, while animateCamera glides smoothly, which feels better for button taps.
With several markers, fit them all on screen with a LatLngBounds builder made from every marker position, plus padding so edge pins are not cut off; call it after the map has been laid out.
Getting the user's current location
Showing the user's own position turns a static map into a useful app. Android treats location as a dangerous permission, so you must ask for it at runtime and the user can refuse. First, declare the permission in the manifest:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Then check whether you already have it before enabling the location layer. The modern way to ask is the Activity Result API with RequestPermission:
private val locationPermissionLauncher = registerForActivityResult(
ActivityResultContracts.RequestPermission()
) { granted -> if (granted) enableMyLocation() else showLocationOffState() }
private fun checkLocationPermission() {
when {
ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED -> enableMyLocation()
shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION) -> showPermissionExplanation()
else -> locationPermissionLauncher.launch(Manifest.permission.ACCESS_FINE_LOCATION)
}
}
That method turns on the blue dot and the built-in re-centre button with googleMap.isMyLocationEnabled = true. The @SuppressLint("MissingPermission") annotation on it is safe only because it runs after the permission check has passed; never enable the location layer without that check, or the app will crash on devices where permission was refused.
To read the coordinates, use FusedLocationProviderClient, and ask for the last known location first, because it returns immediately when a recent fix exists:
private lateinit var fusedLocationClient: FusedLocationProviderClient
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
fusedLocationClient.lastLocation.addOnSuccessListener { location ->
if (location != null) {
val current = LatLng(location.latitude, location.longitude)
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(current, 16f))
}
}
The last known location can be null, for example on a fresh emulator. Show your default place instead of crashing. For live tracking, request updates with a LocationRequest and a LocationCallback, and stop them when the screen closes so you do not drain the battery.
When permission is denied, do not keep asking on every launch. Explain which feature needs location, keep the map usable with a default place, and offer a button that opens the app settings; if permission was permanently denied, the system will not show your request dialog again.
Drawing a route polyline
A route is a polyline: a line drawn through a list of points. Ask the Directions API for a route between an origin and a destination, take the encoded polyline string from the response, decode it into a list of LatLng points, and draw it with addPolyline.
Each route in the Directions API response has an overview_polyline object with a points string, a compact encoding of the route path. Android does not ship a decoder, so add a small decode function to your project.
The request is a plain HTTPS call, made with Retrofit or any HTTP client, carrying the origin, the destination, a travel mode, and your API key as a query parameter. Keep the key restricted if the app will be distributed widely.
Once you have the decoded points, drawing the route is simple:
val routePoints: List<LatLng> = decodePolyline(encodedPoints)
googleMap.addPolyline(
PolylineOptions()
.addAll(routePoints)
.width(10f)
.color(Color.BLUE)
)
Then fit the camera to the route and add markers at the origin and destination. Cache the decoded points and only request a new route when the origin or destination changes. If the API returns no routes, show "No driving route found".
Customising the map
The map type switches between normal, satellite, terrain and hybrid views; offer it as a setting rather than hard-coding one type, since satellite looks good in demos but is harder to read for navigation.
The UI settings control the built-in buttons and gestures; turn off any your app replaces with its own buttons. For a distinct look, apply a map style from a JSON file in res/raw.
Common mistakes
1. Using an unrestricted API key. An unrestricted key can be copied from your app and used by anyone. Restrict it to your package name and SHA-1, add both debug and release fingerprints, and keep it in local.properties.
2. Forgetting to enable the Maps SDK for Android. Creating a key is not enough. If the SDK is not enabled for your Cloud project, the map stays blank and Logcat reports an API-not-enabled error.
3. Calling map methods before the map is ready. The map loads asynchronously: any addMarker, moveCamera or style call before onMapReady fires will crash or silently do nothing. Put all map setup inside onMapReady.
4. Requesting location without a runtime permission check. Declaring ACCESS_FINE_LOCATION in the manifest is not enough. Request it at runtime, handle a refusal gracefully, and never enable the location layer before permission is granted.
5. Using the last known location without a null check. lastLocation is null when no recent fix exists, which is common on emulators. Always check for null and fall back to a default place.
6. Hard-coding the API key in source code or the manifest. A key committed to Git stays in history even after you delete it. If a key has ever been committed, revoke it in the Cloud Console and create a new one.
Frequently asked questions
Do I need a billing account to use Google Maps in my Android app?
Yes. Google requires a billing account on your Cloud project before the Maps SDK can be used. Check the current Google Maps Platform pricing page for the latest limits and rates.
Why is my map blank even though the app runs?
The three usual causes are a missing or wrong API key, the Maps SDK for Android not being enabled in the Cloud Console, and a key restriction that does not match your package name or SHA-1 fingerprint.
Why does the map work in the debug build but not the release build?
Debug and release builds have different SHA-1 fingerprints. If the restriction lists only the debug fingerprint, the release build is rejected, so add both.
Can I test maps on an emulator?
Yes, if the emulator image includes Google Play services; a plain image without them will not render the map. Set a fake location in the emulator's extended controls to test the location flow.
How do I show many markers without cluttering the map?
Use the Maps SDK utility library's clustering feature, which collapses close markers into a numbered cluster that expands as you zoom in.
Does the Maps SDK work offline?
No. Map tiles download as the user pans and zooms, so a device without internet shows a blank or partly loaded map. Show a clear offline message and keep place data cached locally.
Limitations
The Maps SDK renders Google's map data, so you depend on Google's coverage for your area; rural areas and new layouts may be less detailed.
Location accuracy varies with hardware and environment: GPS is accurate outdoors but weak indoors, and network-based location is approximate. Treat every reading as an estimate with an accuracy radius, not an exact point.
Routes from the Directions API reflect road data and traffic estimates at the time of the request; they are guidance, not a guarantee of travel time. Map and directions usage is metered, so load the map once, cache decoded routes, and request new data only when the inputs change.
Get your Android app built with Projectech
If your app idea needs maps, location tracking, routes or a full Android build and you would rather have it built for you, Projectech can build it. Message Projectech on WhatsApp with your app idea, the map features you need, and your deadline, and you will get a quotation stating the scope, price and delivery timeline: https://wa.me/918275719193. Share the screens you need and whether the app also needs login, a database or an admin panel, so the quotation matches what you actually want built.