Select platform: [Android](https://developers.google.com/maps/documentation/android-sdk/map.md.txt "View this page for the Android platform docs.") [iOS](https://developers.google.com/maps/documentation/ios-sdk/map.md.txt "View this page for the iOS platform docs.") [JavaScript](https://developers.google.com/maps/documentation/javascript/maptypes.md.txt "View this page for the JavaScript platform docs.")

<br />

This topic describes how to add a basic map to an Android app after you have
[configured the project](https://developers.google.com/maps/documentation/android-sdk/config.md.txt) to use the Maps SDK for Android. After
adding a map, you can change the [map type and features](https://developers.google.com/maps/documentation/android-sdk/configure-map.md.txt).

> [!NOTE]
> **Note:** To quickly create an app that already includes a basic map, you can use the Google Maps template in Android Studio. See the [quickstart](https://developers.google.com/maps/documentation/android-sdk/start.md.txt) for details.

## Overview

The Maps SDK for Android provides several classes your app can use to
manage the lifecycle, functionality, and data of a map. The classes support user
interactions based on the Android UI model, such as setting the initial state of
the map, and responding to gesture input from the user at runtime.

The main interface and classes for handling maps:

- [`GoogleMap`](https://developers.google.com/maps/documentation/android-sdk/reference/com/google/android/libraries/maps/GoogleMap.md.txt) --- The entry point for managing the underlying map
  features and data. Your app can only access a `GoogleMap` object after it has
  been retrieved from a `SupportMapFragment` or `MapView` object.

- [`SupportMapFragment`](https://developers.google.com/maps/documentation/android-sdk/reference/com/google/android/libraries/maps/SupportMapFragment.md.txt) --- A [fragment](https://developer.android.com/guide/fragments) for
  managing the lifecycle of a `GoogleMap` object.

- [`MapView`](https://developers.google.com/maps/documentation/android-sdk/reference/com/google/android/libraries/maps/MapView.md.txt) --- A [view](https://developer.android.com/reference/android/view/View.html) for managing the lifecycle of a
  `GoogleMap` object.

- [`OnMapReadyCallback`](https://developers.google.com/maps/documentation/android-sdk/reference/com/google/android/libraries/maps/OnMapReadyCallback.md.txt) --- A callback interface that
  handles events and user interaction for the `GoogleMap` object.


A `GoogleMap` object automatically performs these operations:

- Connecting to the Google Maps service.
- Downloading map tiles.
- Displaying tiles on the device screen.
- Displaying various controls such as pan and zoom.
- Responding to pan and zoom gestures by moving the map and zooming in or out.


To use a `GoogleMap` object in your app, you must use either a
`SupportMapFragment` or `MapView` object as a container object for the map and
then retrieve the `GoogleMap` object from the container. Because the container
classes derive from either an Android fragment or view, they provide the map
with the lifecycle management and UI capabilities of their Android base classes.
The `SupportMapFragment` class is the more modern and common container for a
`GoogleMap` object.

## View the code

The following code is from the full Java activity used in this topic when adding
a fragment statically. The Android project was created from the Empty project
template, and then updated based on the [project configuration](https://developers.google.com/maps/documentation/android-sdk/config.md.txt) guide.
After performing the steps in this topic, your code may differ based on the
project template.
<button type="button" class="button-red button expand-control">Show / Hide Code</button>

```java
  package com.example.mapsetup;

  import androidx.appcompat.app.AppCompatActivity;

  import android.os.Bundle;

  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.LatLng;
  import com.google.android.gms.maps.model.MarkerOptions;

  // Implement OnMapReadyCallback.
  public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {

      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          // Set the layout file as the content view.
          setContentView(R.layout.activity_main);

          // Get a handle to the fragment and register the callback.
          SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                  .findFragmentById(R.id.map);
          mapFragment.getMapAsync(this);

      }

      // Get a handle to the GoogleMap object and display marker.
      @Override
      public void onMapReady(GoogleMap googleMap) {
          googleMap.addMarker(new MarkerOptions()
                  .position(new LatLng(0, 0))
                  .title("Marker"));
      }
  }
```

## To add a map


This section describes how to add a basic map by using a fragment as a map
container; however, you can use a view instead. For an example, see
[RawMapViewDemoActivity](https://github.com/googlemaps-samples/android-samples/blob/main/ApiDemos/project/java-app/src/main/java/com/example/mapdemo/RawMapViewDemoActivity.java)
on Github.

The basic steps:

1. To get the SDK, obtain an API key, and add the required frameworks,
   follow the steps in:

   1. [Set Up in the Google Cloud console](https://developers.google.com/maps/documentation/android-sdk/cloud-setup)

   2. [Use an API key](https://developers.google.com/maps/documentation/android-sdk/get-api-key.md.txt)

   3. [Set up an Android Studio project](https://developers.google.com/maps/documentation/android-sdk/config.md.txt)

2. Add a `SupportMapFragment` object to the activity that will handle the map.
   You can add the fragment statically or dynamically.

3. Implement the `OnMapReadyCallback` interface.

4. Set the layout file as the content view.

5. If you added the fragment statically, get a handle to the fragment.

6. Register the callback.

7. Get a handle to the `GoogleMap` object.

### Add a SupportMapFragment object

You can add a `SupportMapFragment` object to your app statically or dynamically.
The simplest way is to add it statically. If you add the fragment dynamically,
you can perform additional actions on the fragment, such as removing and
replacing it at runtime.

#### To add a fragment Statically

In the [layout file](https://developer.android.com/guide/topics/ui/declaring-layout) of the activity that will handle the map:

1. Add a `fragment` element.
2. Add the name declaration `xmlns:map="http://schemas.android.com/apk/res-auto"`. This enables the use of `maps` [custom XML attributes](https://developers.google.com/maps/documentation/android-sdk/configure-map.md.txt#static).
3. In the `fragment` element, set the `android:name` attribute to `com.google.android.gms.maps.SupportMapFragment`.
4. In the `fragment` element, add the `android:id` attribute and set it to the the R.id.map [resource ID](https://developer.android.com/guide/topics/resources/more-resources#Id) (`@+id/map`).

For example, here's a complete layout file that includes a `fragment` element:

```xml
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
```

#### To add a fragment dynamically

In the activity:

1. Create a `SupportMapFragment` instance.
2. Commit a transaction that adds the fragment to the activity. For more info, see [Fragment Transactions](https://developer.android.com/guide/fragments/transactions).

For example:


### Kotlin

```kotlin
val mapFragment = SupportMapFragment.newInstance()
supportFragmentManager
    .beginTransaction()
    .add(R.id.map_container, mapFragment)
    .commit()

      
```

### Java

```java
SupportMapFragment mapFragment = SupportMapFragment.newInstance();
FragmentTransaction fragmentTransaction =
        getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.map_container, mapFragment);
fragmentTransaction.commit();

      
```

<br />

### Implement the `OnMapReadyCallback` interface

Update the activity declaration as follows:


### Kotlin

```kotlin
class MainActivity :
    AppCompatActivity(),
    OnMapReadyCallback {

    // ...
}

      
```

### Java

```java
class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
    // ...
}

      
```

<br />

### Set the content view

In the [`onCreate`](http://developer.android.com/reference/android/app/Activity.html#onCreate(android.os.Bundle)) method of your activity, call the
`setContentView` method and set the layout file as the content view.

For example, if the layout file is named `main.xml`:


### Kotlin

```kotlin
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.main)
}

      
```

### Java

```java
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}


      
```

<br />

### Get a handle to the fragment and register the callback

1. To get a handle to the fragment, call the
   [`FragmentManager.findFragmentById`](http://developer.android.com/reference/android/app/FragmentManager.html#findFragmentById(int)) method and pass it
   the resource ID of the fragment in your layout file. If you **added the fragment
   dynamically**, skip this step because you already retrieved the handle.

2. Call the [`getMapAsync`](https://developers.google.com/maps/documentation/android-sdk/reference/com/google/android/libraries/maps/MapFragment.md.txt#public-void-getmapasync-onmapreadycallback-callback) method to set the callback on the
   fragment.

For example, if you added the fragment statically:


### Kotlin

```kotlin
val mapFragment = supportFragmentManager
    .findFragmentById(R.id.map) as SupportMapFragment
mapFragment.getMapAsync(this)

      
```

### Java

```java
SupportMapFragment mapFragment =
        (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);

      
```

<br />

### Get a handle to the `GoogleMap` object

Use the [`onMapReady`](https://developers.google.com/maps/documentation/android-sdk/reference/com/google/android/libraries/maps/OnMapReadyCallback.md.txt#public-abstract-void-onmapready-googlemap-googlemap) callback method to get a handle to the
`GoogleMap`object. The callback is triggered when the map is ready to receive
user input. It provides a non-null instance of the `GoogleMap` class that you
can use to update the map.

In this example the `onMapReady` callback retrieves a handle to the `GoogleMap`
object and then a [marker](https://developers.google.com/maps/documentation/android-sdk/map-with-marker.md.txt) is added to the map:


### Kotlin

```kotlin
override fun onMapReady(googleMap: GoogleMap) {
    googleMap.addMarker(
        MarkerOptions()
            .position(LatLng(0.0, 0.0))
            .title("Marker"),
    )
}

      
```

### Java

```java
@Override
public void onMapReady(GoogleMap googleMap) {
    googleMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}


      
```

<br />

![Screenshot with the map and marker centered on Null Island.](https://developers.google.com/static/maps/documentation/android-sdk/images/add-map-screenshot.png)

When you successfully [build and run](https://developer.android.com/studio/run) the app, it will display a map
with a marker on Null Island (zero degrees latitude and zero degrees longitude).

View the code for the complete activity:

[View
Complete Activity](https://developers.google.com/maps/documentation/android-sdk/map#view_the_code)

*** ** * ** ***

## What's next

After you complete these steps, you can
[configure the map settings](https://developers.google.com/maps/documentation/android-sdk/configure-map.md.txt).