Adding a Map with a Marker

This tutorial shows you how to add a Google map to your Android app. The map includes a marker, also called a pin, to indicate a specific location.

Follow the tutorial to build an Android app using the Maps SDK for Android. The recommended development environment is Android Studio.

Get the code

Clone or download the Google Maps Android API v2 Samples repository from GitHub.

Set up your development project

Follow these steps to create the tutorial project in Android Studio.

  1. Download and install Android Studio.
  2. Add the Google Play services package to Android Studio.
  3. Clone or download the Google Maps Android API v2 Samples repository if you didn't do that when you started reading this tutorial.
  4. Import the tutorial project:

    • In Android Studio, select File > New > Import Project.
    • Go to the location where you saved the Google Maps Android API v2 Samples repository after downloading it.
    • Find the MapWithMarker project at this location:
    • Select the project directory, then click OK. Android Studio now builds your project, using the Gradle build tool.

Get an API key and enable the necessary APIs

To complete this tutorial, you need a Google API key that's authorized to use the Maps SDK for Android.

Click the button below to get a key and activate the API.

Get Started

For more details, see the full guide to getting an API key.

Add the API key to your app

  1. Edit your project's file.
  2. Paste your API key into the value of the GOOGLE_MAPS_API_KEY property. When you build your app, Gradle copies the API key into the app's Android manifest, as explained below.


Build and run your app

  1. Connect an Android device to your computer. Follow the instructions to enable developer options on your Android device and configure your system to detect the device. (Alternatively, you can use the Android Virtual Device (AVD) Manager to configure a virtual device. When choosing an emulator, make sure you pick an image that includes the Google APIs. For more details, see the getting started guide.)
  2. In Android Studio, click the Run menu option (or the play button icon). Choose a device as prompted.

Android Studio invokes Gradle to build the app, and then runs the app on the device or on the emulator. You should see a map with a marker pointing at Sydney on the east coast of Australia, similar to the image on this page.


Understand the code

This part of the tutorial explains the most significant parts of the MapWithMarker app, to help you understand how to build a similar app.

Check your Android manifest

Note the following elements in your app's AndroidManifest.xml file:

  • Add a meta-data element to embed the version of Google Play services that the app was compiled with.

        android:value="@integer/google_play_services_version" />
  • Add a meta-data element specifying your API key. The sample accompanying this tutorial maps the value for the API key to a string google_maps_key. When you build your app, Gradle copies the API key from your project's file to the string value.

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

    To see how the API key maps to the string value, take a look at your app's build.gradle. It contains the following line that maps the string google_maps_key to the gradle property GOOGLE_MAPS_API_KEY:

      resValue "string", "google_maps_key",
              (project.findProperty("GOOGLE_MAPS_API_KEY") ?: "")

Below is an example of a full manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""


            android:value="@integer/google_play_services_version" />

             The API key for Google Maps-based APIs.
            android:value="@string/maps_api_key" />

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />


Add a map

Display a map, using the Maps SDK for Android.

  1. Add a <fragment> element to your activity's layout file, activity_maps.xml. This element defines a SupportMapFragment to act as a container for the map and to provide access to the GoogleMap object. The tutorial uses the Android support library version of the map fragment, to ensure backward compatibility with earlier versions of the Android framework.

    <fragment xmlns:android=""
        tools:context="com.example.mapwithmarker.MapsMarkerActivity" />
  2. In your activity's onCreate() method, set the layout file as the content view. Get a handle to the map fragment by calling FragmentManager.findFragmentById(). Then use getMapAsync() to register for the map callback:


    protected void onCreate(Bundle savedInstanceState) {
        // Retrieve the content view that renders the map.
        // Get the SupportMapFragment and request notification
        // when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()


    override fun onCreate(savedInstanceState: Bundle?) {
        // Retrieve the content view that renders the map.
        if (getString(R.string.maps_api_key).isEmpty()) {
            Toast.makeText(this, "Add your own API key in MapWithMarker/app/ as MAPS_API_KEY=YOUR_API_KEY", Toast.LENGTH_LONG).show()
        // Get the SupportMapFragment and request notification when the map is ready to be used.
        val mapFragment = supportFragmentManager.findFragmentById( as? SupportMapFragment
  3. Implement the OnMapReadyCallback interface and override the onMapReady() method, to set up the map when the GoogleMap object is available:


    public class MapsMarkerActivity extends AppCompatActivity
            implements OnMapReadyCallback {
        // ...
        public void onMapReady(GoogleMap googleMap) {
            LatLng sydney = new LatLng(-33.852, 151.211);
            googleMap.addMarker(new MarkerOptions()
                .title("Marker in Sydney"));


    class MapsMarkerActivity : AppCompatActivity(), OnMapReadyCallback {
        // ...
        override fun onMapReady(googleMap: GoogleMap?) {
            googleMap?.apply {
                val sydney = LatLng(-33.852, 151.211)
                        .title("Marker in Sydney")

By default, the Maps SDK for Android displays the content of the info window when the user taps a marker. There's no need to add a click listener for the marker if you’re happy to use the default behavior.

Next steps

Learn more about the map object and what you can do with markers.