Intermediate 20 min Java Full project

Google Maps in Android Studio: Maps SDK Tutorial (Java)

Add a Google Map to your app, place markers and move the camera to any location. You will set up a Maps API key and the Maps SDK the right way.

What you'll learn

  • Create a Google Maps API key
  • Add a map with SupportMapFragment
  • Place markers and move the camera
  • Keep your API key out of your code

Before you start

  • Android Studio (any recent version)
  • Basic Java: variables, methods and if/else
  • A Google Cloud account for the Maps API key
  • An emulator or an Android phone

How it fits together

  1. 1API key in local.properties
  2. 2Manifest meta-data
  3. 3SupportMapFragment
  4. 4onMapReady
  5. 5Markers and camera

Build it step by step

1. Get a Maps API key

In the Google Cloud console, create a project, enable Maps SDK for Android, and create an API key. Restrict it to Android apps with your package name and SHA-1 so nobody else can use it.

2. Create the project

In Android Studio choose New Project → Empty Views Activity, with Language Java and Minimum SDK API 24. Then add implementation("com.google.android.gms:play-services-maps:20.1.0") to app/build.gradle.kts and press Sync Now.

3. Keep the key out of your code

Put MAPS_API_KEY=your-key in local.properties, which Git ignores. app/build.gradle.kts reads it and fills the ${MAPS_API_KEY} placeholder in the manifest's com.google.android.geo.API_KEY meta-data. Your key never ends up on GitHub.

4. Add the map to the layout

Add a FragmentContainerView with android:name="com.google.android.gms.maps.SupportMapFragment". The Maps SDK draws the map inside it.

5. Wait for the map

Maps load in the background. Implement OnMapReadyCallback, call getMapAsync(this), and do everything in onMapReady.

6. Add markers

Create each place as a LatLng(latitude, longitude) and call addMarker. Give markers a title, a snippet and a colour with BitmapDescriptorFactory.defaultMarker.

7. Move the camera

LatLngBounds with newLatLngBounds fits all the markers on screen. animateCamera with newLatLngZoom flies smoothly to one place.

8. Change the map style

Switch between MAP_TYPE_NORMAL, MAP_TYPE_HYBRID and MAP_TYPE_TERRAIN with setMapType.

9. Run it

Run on a phone or an emulator with Google Play. Tap Fly to Pune and Map style.

If something goes wrong

  • A grey map with the Google logo: the key is missing, wrong, or the Maps SDK isn't enabled for it. Check Logcat for "Authorization failure".
  • The map never appears on the emulator: use a system image with Google Play.
  • Your key is on GitHub: delete it in the console, create a new one, and keep it in local.properties.

What's improved over the video

The API key stays out of your code and off GitHub, all markers fit on screen at launch, and there are buttons to fly to a city and change the map style.

Required setup

These are additions to an existing project—not replacement project files.

Add the Maps SDK

Add these lines

Where: app/build.gradle.kts

Add this line inside dependencies, then Sync Now.

app/build.gradle.kts
implementation("com.google.android.gms:play-services-maps:20.1.0")

Add your API key

Add these lines

Where: local.properties (project root)

Paste your key here. local.properties is never committed, so your key stays private.

local.properties (project root)
MAPS_API_KEY=your-key-here

Pass the key to the manifest

Merge additions

Where: app/build.gradle.kts

Reads the key from local.properties and fills ${MAPS_API_KEY} in the manifest.

app/build.gradle.kts
import java.util.Properties

// inside android { defaultConfig { ... } }
val localProperties = Properties().apply {
    rootProject.file("local.properties").takeIf { it.exists() }?.inputStream()?.use { load(it) }
}
manifestPlaceholders["MAPS_API_KEY"] = localProperties.getProperty("MAPS_API_KEY", "")

Application code

Select a file to see its code and where it belongs.

This code uses package com.example.googlemaps. Change that first line to your own package, the one at the top of your MainActivity.

Replace fileapp/src/main/java/com/example/googlemaps/MainActivity.java

Waits for the map, adds three markers, fits them on screen, and handles the two buttons.

MainActivity.java
package com.example.googlemaps;

import android.os.Bundle;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.MarkerOptions;

/**
 * Shows a Google Map with a few markers. Tap a button to fly to a place or change the map style.
 * The map loads asynchronously: everything happens in onMapReady.
 */
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
    private static final LatLng MUMBAI = new LatLng(19.0760, 72.8777);
    private static final LatLng PUNE = new LatLng(18.5204, 73.8567);
    private static final LatLng BENGALURU = new LatLng(12.9716, 77.5946);

    private GoogleMap map;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Screens.setUp(this, R.layout.activity_main);

        SupportMapFragment mapFragment =
                (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        if (mapFragment != null) mapFragment.getMapAsync(this);

        Button pune = findViewById(R.id.btnPune);
        Button style = findViewById(R.id.btnStyle);
        pune.setOnClickListener(v -> {
            if (map != null) map.animateCamera(CameraUpdateFactory.newLatLngZoom(PUNE, 12));
        });
        style.setOnClickListener(v -> {
            if (map == null) return;
            // Cycle through the built-in map types.
            int next = map.getMapType() == GoogleMap.MAP_TYPE_NORMAL ? GoogleMap.MAP_TYPE_HYBRID
                    : map.getMapType() == GoogleMap.MAP_TYPE_HYBRID ? GoogleMap.MAP_TYPE_TERRAIN
                    : GoogleMap.MAP_TYPE_NORMAL;
            map.setMapType(next);
        });
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        map = googleMap;
        map.getUiSettings().setZoomControlsEnabled(true);

        map.addMarker(new MarkerOptions().position(MUMBAI).title("Mumbai").snippet("The city of dreams"));
        map.addMarker(new MarkerOptions().position(PUNE).title("Pune")
                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_VIOLET)));
        map.addMarker(new MarkerOptions().position(BENGALURU).title("Bengaluru")
                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));

        // Start with all three markers in view.
        LatLngBounds bounds = new LatLngBounds.Builder().include(MUMBAI).include(PUNE).include(BENGALURU).build();
        findViewById(R.id.map).post(() -> map.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 120)));
    }
}

Full source code