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

## Controls Overview

The maps displayed through the Maps JavaScript API contain UI
elements to allow user interaction with the map. These elements are known as
*controls* and you can include variations of these controls in your
application. Alternatively, you can do nothing and let the
Maps JavaScript API handle all control behavior.

The following map shows the default set of controls displayed by the
Maps JavaScript API:
<iframe src="https://maps-docs-team.web.app/samples/map-simple/dist/" allow="fullscreen; "></iframe>

Clockwise from top left: Map Type, Fullscreen, Camera, Street View,
Keyboard shortcuts.

Below is a list of the full set of controls you can use in your maps:

- The ***Map Type control*** is available in a drop-down or horizontal button bar style, allowing the user to choose a map type (`ROADMAP`, `SATELLITE`, `HYBRID`, or `TERRAIN`). This control appears by default in the top left corner of the map.
- The ***Fullscreen control*** offers the option to open the map in fullscreen mode. This control is enabled by default on desktop and mobile devices. **Note:** iOS doesn't support the fullscreen feature. The fullscreen control is therefore not visible on iOS devices.
- The ***Camera control*** features both zoom and pan controls.
- The ***Street View control*** contains a Pegman icon which can be dragged onto the map to enable Street View. This control appears by default near the bottom right of the map.
- The ***Rotate control*** provides a combination of tilt and rotate options for maps containing 3D imagery. This control appears by default near the bottom right of the map. See [3D overview](https://developers.google.com/maps/documentation/javascript/3d/overview.md.txt) for more information.
- The ***Scale control*** displays a map scale element. This control is disabled by default.
- The ***Keyboard shortcuts control*** displays a list of keyboard shortcuts for interacting with the map.

You don't access or modify these map controls directly. Instead, you
modify the map's [`MapOptions`](https://developers.google.com/maps/documentation/javascript/reference/map.md.txt#MapOptions)
fields which affect the visibility and presentation of controls. You can adjust control
presentation upon instantiating your map (with appropriate `MapOptions`) or modify a
map dynamically by calling [`setOptions()`](https://developers.google.com/maps/documentation/javascript/reference/map.md.txt#Map.setOptions)
to change the map's options.

Not all of these controls are enabled by default. To learn about default UI
behavior (and how to modify such behavior), see [The
Default UI](https://developers.google.com/maps/documentation/javascript/controls#DefaultUI) below.

## The Default UI

By default, all the controls disappear if the map is too small (200x200px).
You can override this behavior by explicitly setting the control to be
visible. See [Adding Controls to the
Map](https://developers.google.com/maps/documentation/javascript/controls#Adding_Controls_to_the_Map).

The behavior and appearance of the controls is the same across mobile and
desktop devices, except for the fullscreen control (see the behavior
described in the [list of controls](https://developers.google.com/maps/documentation/javascript/controls.md.txt#all-controls)).

Additionally, keyboard handling is on by default on all devices.

## Disable the Default UI

You may want to turn off the API's default UI buttons entirely. To do so,
set the map's `disableDefaultUI` property (within the
`MapOptions` object) to `true`. This property
disables any UI control buttons from the Maps JavaScript API. It
does not, however, affect mouse gestures or keyboard shortcuts on the base
map, which are controlled by the `gestureHandling` and
`keyboardShortcuts` properties respectively.

The following code disables the UI buttons:

### TypeScript

```typescript
innerMap.setOptions({
    // Disable the default UI.
    disableDefaultUI: true,
});
```

### JavaScript

```javascript
innerMap.setOptions({
    // Disable the default UI.
    disableDefaultUI: true,
});
```
[View example](https://developers.google.com/maps/documentation/javascript/examples/control-disableUI.md.txt)

### Try Sample

[JSFiddle.net](https://jsfiddle.net/gh/get/library/pure/googlemaps-samples/js-api-samples/tree/main/dist/samples/control-disableUI/jsfiddle)

## Add Controls to the Map

You may want to tailor your interface by removing, adding, or modifying UI
behavior or controls and ensure that future updates don't alter this
behavior. If you want to only add or modify existing behavior, you need to
ensure that the control is explicitly added to your application.

Some controls appear on the map by default while others won't appear
unless you specifically request them. Adding or removing controls from the
map is specified in the following `MapOptions` object's
fields, which you set to `true` to make them visible or set to
`false` to hide them:

```javascript
{
  cameraControl: boolean,
  mapTypeControl: boolean,
  scaleControl: boolean,
  streetViewControl: boolean,
  rotateControl: boolean,
  fullscreenControl: boolean
}
```

By default, all the controls disappear if the map is smaller than 200x200px.
You can override this behavior by explicitly setting the control to be
visible. For example, the following table shows whether the camera control is
visible or not, based on the map size and the setting of the
`cameraControl` field:

| Map size | `cameraControl` | Visible? |
|---|---|---|
| Any | `false` | No |
| Any | `true` | Yes |
| \>= 200x200px | `undefined` | Yes |
| \< 200x200px | `undefined` | No |

The following example sets the map to hide the Camera control and display
the Scale control. Note that we don't explicitly disable the default UI, so
these modifications are additive to the default UI behavior.

### TypeScript

```typescript
innerMap.setOptions({
    cameraControl: false,
    scaleControl: true,
});
```

### JavaScript

```javascript
innerMap.setOptions({
    cameraControl: false,
    scaleControl: true,
});
```
[View example](https://developers.google.com/maps/documentation/javascript/examples/control-simple.md.txt)

### Try Sample

[JSFiddle.net](https://jsfiddle.net/gh/get/library/pure/googlemaps-samples/js-api-samples/tree/main/dist/samples/control-simple/jsfiddle)

## Control Options

Several controls are configurable, allowing you to alter their behavior or
change their appearance. The ***Map Type control***, for
example, may appear as a horizontal bar or a drop-down menu.

These controls are modified by altering appropriate control *options*
fields within the `MapOptions` object upon creation of the map.

For example, options for altering the Map Type control are indicated in the
`mapTypeControlOptions` field. The Map Type control may appear in
one of the following `style` options:

- `google.maps.MapTypeControlStyle.HORIZONTAL_BAR` displays the array of controls as buttons in a horizontal bar as is shown on Google Maps.
- `google.maps.MapTypeControlStyle.DROPDOWN_MENU` displays a single button control allowing you to select the map type using a drop-down menu.
- `google.maps.MapTypeControlStyle.DEFAULT` displays the default behavior, which depends on screen size and may change in future versions of the API.

Note that if you do modify any control options, you should explicitly enable
the control as well by setting the appropriate `MapOptions` value
to `true`. For example, to set a Map Type control
to exhibit the `DROPDOWN_MENU` style, use the following code within
the `MapOptions` object:

```javascript
  ...
  mapTypeControl: true,
  mapTypeControlOptions: {
    style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
  }
  ...
```

The following example demonstrates how to change the default position and
style of controls.

### TypeScript

```typescript
innerMap.setOptions({
    mapTypeControl: true,
    mapTypeControlOptions: {
        style: MapTypeControlStyle.DROPDOWN_MENU,
        mapTypeIds: [MapTypeId.ROADMAP, MapTypeId.TERRAIN],
        position: ControlPosition.TOP_CENTER,
    },
});
```

### JavaScript

```javascript
innerMap.setOptions({
    mapTypeControl: true,
    mapTypeControlOptions: {
        style: MapTypeControlStyle.DROPDOWN_MENU,
        mapTypeIds: [MapTypeId.ROADMAP, MapTypeId.TERRAIN],
        position: ControlPosition.TOP_CENTER,
    },
});
```
[View example](https://developers.google.com/maps/documentation/javascript/examples/control-options.md.txt)

### Try Sample

[JSFiddle.net](https://jsfiddle.net/gh/get/library/pure/googlemaps-samples/js-api-samples/tree/main/dist/samples/control-options/jsfiddle)

Controls are typically configured upon creation of the map. However,
you may alter the presentation of controls dynamically by
calling the `Map`'s `setOptions()` method,
passing it new control options.

### Modify Controls

You specify a control's presentation when you create your map
through fields within the map's `MapOptions` object. These
fields are denoted below:

- `cameraControl` enables/disables the camera control that lets the user zoom and pan the map. This control is visible by default on all maps. The `cameraControlOptions` field additionally specifies the [`CameraControlOptions`](https://developers.google.com/maps/documentation/javascript/reference/control.md.txt#CameraControlOptions) to use for this control.
- `mapTypeControl` enables/disables the Map Type control that lets the user toggle between map types (such as Map and Satellite). By default, this control is visible and appears in the top left corner of the map. The `mapTypeControlOptions` field additionally specifies the [`MapTypeControlOptions`](https://developers.google.com/maps/documentation/javascript/reference/control.md.txt#MapTypeControlOptions) to use for this control.
- `streetViewControl` enables/disables the Pegman control that lets the user activate a Street View panorama. By default, this control is visible and appears near the bottom right of the map. The `streetViewControlOptions` field additionally specifies the [`StreetViewControlOptions`](https://developers.google.com/maps/documentation/javascript/reference/control.md.txt#StreetViewControlOptions) to use for this control.
- `rotateControl` enables/disables the appearance of a Rotate control for controlling the orientation of 3D imagery. By default, the control's presence is determined by the presence or absence of 3D imagery for the given map type at the current zoom and location. You may alter the control's behavior by setting the map's `rotateControlOptions` to specify the [`RotateControlOptions`](https://developers.google.com/maps/documentation/javascript/reference/control.md.txt#RotateControlOptions) to use. The control will only appear on 3D base maps.
- `scaleControl` enables/disables the Scale control that provides a map scale. By default, this control is not visible. When enabled, it will always appear in the bottom right corner of the map. The `scaleControlOptions` additionally specifies the [`ScaleControlOptions`](https://developers.google.com/maps/documentation/javascript/reference/control.md.txt#ScaleControlOptions) to use for this control.
- `fullscreenControl` enables/disables the control that opens the map in fullscreen mode. By default, this control is enabled by default on desktop and Android devices. When enabled, the control appears near the top right of the map. The `fullscreenControlOptions` additionally specifies the [`FullscreenControlOptions`](https://developers.google.com/maps/documentation/javascript/reference/control.md.txt#FullscreenControlOptions) to use for this control.

Note that you may specify options for controls you initially disable.

### Control Positioning

Most of the control options contain a `position` property
(of type `ControlPosition`) which indicates where on the map to
place the control. Positioning of these controls is not absolute. Instead,
the API will lay out the controls intelligently by flowing them around
existing map elements, or other controls, within given constraints (such as
the map size).

There are two flavors of control positions: legacy and logical. Usage of
[logical values](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_logical_properties_and_values)
is recommended in order to be able to automatically support both left-to-right (LTR) and
right-to-left (RTL) layout contexts.
[See the reference guide](https://developers.google.com/maps/documentation/javascript/reference/control.md.txt#ControlPosition).

The following tables show the supported control positions in LTR and RTL
contexts.

### LTR positions

| Position (LTR Context) | Logical Constant (Recommended) | Legacy Constant |
|---|---|---|
| **Top Left** | `BLOCK_START_INLINE_START` | `TOP_LEFT` |
| **Top Center** | `BLOCK_START_INLINE_CENTER` | `TOP_CENTER` |
| **Top Right** | `BLOCK_START_INLINE_END` | `TOP_RIGHT` |
| **Left Top** | `INLINE_START_BLOCK_START` | `LEFT_TOP` |
| **Left Center** | `INLINE_START_BLOCK_CENTER` | `LEFT_CENTER` |
| **Left Bottom** | `INLINE_START_BLOCK_END` | `LEFT_BOTTOM` |
| **Right Top** | `INLINE_END_BLOCK_START` | `RIGHT_TOP` |
| **Right Center** | `INLINE_END_BLOCK_CENTER` | `RIGHT_CENTER` |
| **Right Bottom** | `INLINE_END_BLOCK_END` | `RIGHT_BOTTOM` |
| **Bottom Left** | `BLOCK_END_INLINE_START` | `BOTTOM_LEFT` |
| **Bottom Center** | `BLOCK_END_INLINE_CENTER` | `BOTTOM_CENTER` |
| **Bottom Right** | `BLOCK_END_INLINE_END` | `BOTTOM_RIGHT` |

### RTL positions

| Position (RTL Context) | Logical Constant (Recommended) | Legacy Constant |
|---|---|---|
| **Top Right** | `BLOCK_START_INLINE_START` | `TOP_RIGHT` |
| **Top Center** | `BLOCK_START_INLINE_CENTER` | `TOP_CENTER` |
| **Top Left** | `BLOCK_START_INLINE_END` | `TOP_LEFT` |
| **Right Top** | `INLINE_START_BLOCK_START` | `RIGHT_TOP` |
| **Right Center** | `INLINE_START_BLOCK_CENTER` | `RIGHT_CENTER` |
| **Right Bottom** | `INLINE_START_BLOCK_END` | `RIGHT_BOTTOM` |
| **Left Top** | `INLINE_END_BLOCK_START` | `LEFT_TOP` |
| **Left Center** | `INLINE_END_BLOCK_CENTER` | `LEFT_CENTER` |
| **Left Bottom** | `INLINE_END_BLOCK_END` | `LEFT_BOTTOM` |
| **Bottom Right** | `BLOCK_END_INLINE_START` | `BOTTOM_RIGHT` |
| **Bottom Center** | `BLOCK_END_INLINE_CENTER` | `BOTTOM_CENTER` |
| **Bottom Left** | `BLOCK_END_INLINE_END` | `BOTTOM_LEFT` |

<iframe src="https://maps-docs-team.web.app/samples/control-positioning-labels/dist/" allow="fullscreen; "></iframe>

Click the labels to toggle the map between LTR and RTL modes.

Note that these positions may coincide with positions of UI elements
whose placements you may not modify (such as copyrights and the Google logo).
In those cases, the controls will flow according to the logic noted for
each position and appear as close as possible to their indicated
position. No guarantees can be made that controls may not overlap given
complicated layouts, though the API will attempt to arrange them intelligently.

The following example shows a basic map with all controls enabled, in
different positions.

### TypeScript

```typescript
const mapElement = document.querySelector('gmp-map')!;
let innerMap: google.maps.Map;

async function init() {
    // Request needed libraries.
    const [{ MapTypeControlStyle }, { ControlPosition }] = await Promise.all([
        google.maps.importLibrary('maps'),
        google.maps.importLibrary('core'),
    ]);

    // Get the inner map.
    innerMap = mapElement.innerMap;

    // Set the map's controls options.
    innerMap.setOptions({
        mapTypeControl: true,
        mapTypeControlOptions: {
            style: MapTypeControlStyle.HORIZONTAL_BAR,
            position: ControlPosition.BLOCK_START_INLINE_CENTER,
        },
        zoomControl: true,
        zoomControlOptions: {
            position: ControlPosition.INLINE_START_BLOCK_CENTER,
        },
        scaleControl: true,
        streetViewControl: true,
        streetViewControlOptions: {
            position: ControlPosition.INLINE_START_BLOCK_START,
        },
        fullscreenControl: true,
    });
}

void init();
```

### JavaScript

```javascript
const mapElement = document.querySelector('gmp-map');
let innerMap;

async function init() {
    // Request needed libraries.
    const [{ MapTypeControlStyle }, { ControlPosition }] = await Promise.all([
        google.maps.importLibrary('maps'),
        google.maps.importLibrary('core'),
    ]);

    // Get the inner map.
    innerMap = mapElement.innerMap;

    // Set the map's controls options.
    innerMap.setOptions({
        mapTypeControl: true,
        mapTypeControlOptions: {
            style: MapTypeControlStyle.HORIZONTAL_BAR,
            position: ControlPosition.BLOCK_START_INLINE_CENTER,
        },
        zoomControl: true,
        zoomControlOptions: {
            position: ControlPosition.INLINE_START_BLOCK_CENTER,
        },
        scaleControl: true,
        streetViewControl: true,
        streetViewControlOptions: {
            position: ControlPosition.INLINE_START_BLOCK_START,
        },
        fullscreenControl: true,
    });
}

void init();
```
[View example](https://developers.google.com/maps/documentation/javascript/examples/control-positioning.md.txt)

### Try Sample

[JSFiddle.net](https://jsfiddle.net/gh/get/library/pure/googlemaps-samples/js-api-samples/tree/main/dist/samples/control-positioning/jsfiddle)

## Custom Controls

As well as modifying the style and position of existing API controls, you
can create your own controls to handle interaction with the user. Controls
are stationary widgets which float on top of a map at absolute positions, as
opposed to *overlays* , which move with the underlying map. More
fundamentally, a control is a `<div>` element which
has an absolute position on the map, displays some UI to the user, and
handles interaction with either the user or the map, usually through an event
handler.

To create your own custom control, few rules are necessary. However, the
following guidelines can act as best practice:

- Define appropriate CSS for the control element(s) to display.
- Handle interaction with the user or the map through event handlers for either map property changes or user events (for example, `'click'` events).
- Create a `<div>` element to hold the control and add this element to the `Map`'s `controls` property.

Each of these concerns is discussed below.

### Draw Custom Controls

How you draw your control is up to you. Generally, we recommend that you
place all of your control presentation within a single
`<div>` element so that you can manipulate your control as
one unit. We will use this design pattern in the samples shown below.

Designing attractive controls requires some knowledge of CSS and DOM
structure. The following code examples show adding a custom control using
both declarative HTML and programmatic methods.

#### Declarative CSS

The following CSS styles provide an appearance that's consistent with
the default controls. Use these styles with both of the examples below:

```css
.streetview-toggle-button {
  align-items: center;
  background: white;
  border: none;
  border-radius: 2px;
  box-shadow: 0 1px 4px -1px rgba(0, 0, 0, 0.3);
  color: rgb(86, 86, 86);
  cursor: pointer;
  display: flex;
  font-family: Roboto, Arial, sans-serif;
  font-size: 18px;
  font-weight: 400;
  height: 40px;
  justify-content: center;
  margin: 10px 0;
  padding: 0 17px;
}

.streetview-toggle-button:hover {
  background: #f4f4f4;
  color: #000;
}
```

#### Declarative HTML

These code snippets show how to create a custom control declaratively.
In the HTML, a DIV with the ID `container` is used to position
the control; this is nested within the `gmp-map` element and
the button is added to the DIV. The `slot` attribute is set to
`control-inline-start-block-start` to position the control in the
top left corner of the map.

```html
<gmp-map
  center="41.027748173921374, -92.41852445367961"
  zoom="13"
  map-id="DEMO_MAP_ID">
  <div id="container" slot="control-inline-start-block-start">
    <input type="button"
    id="streetview-toggle-button"
    class="button"
    value="Click this button" />
  </div>
</gmp-map>
```

In the JavaScript, `getElementById()` is used to find the DIV and button, an event
listener is added to the button, and the button is appended to the DIV.

```javascript
// Create a DIV to attach the control UI to the Map.
const container = document.getElementById("container");

// Get the control button from the HTML page.
const controlButton = document.getElementById("streetview-toggle-button");

// Add a click event listener.
controlButton.addEventListener("click", () => {
  window.alert("You clicked the button!");
});

// Add the control to the DIV.
container.append(controlButton);
```

#### Programmatic JavaScript

This code snippet demonstrates programmatically creating a button control.
The CSS styles are defined above.

```javascript
// Create a DIV to attach the control UI to the Map.
const container = document.getElementById("container");

// Position the control in the top left corner of the map.
container.slot = "control-block-start-inline-start";

// Create the control.
const controlButton = document.createElement("button");
controlButton.classList.add("streetview-toggle-button");
controlButton.textContent = "Click this button";
controlButton.type = "button";

// Add a click event listener.
controlButton.addEventListener("click", () => {
  window.alert("You clicked the button!");
});

// Add the control to the DIV.
container.append(controlButton);
```

### Handle Events from Custom Controls

For a control to be useful, it must actually do something. What the control
does is up to you. The control may respond to user input, or it may respond
to changes in the `Map`'s state.

For responding to user input, use `addEventListener()`, which handles [supported DOM events](https://en.wikipedia.org/wiki/DOM_events). The
following code snippet adds a listener for the browser's `'click'`
event. Note that this event is received from the DOM, not from the map.

```javascript
// Setup the click event listener: set the map to center on Chicago
var chicago = {lat: 41.850, lng: -87.650};

controlButton.addEventListener('click', () => {
  map.setCenter(chicago);
});
```

### Make Custom Controls Accessible

To ensure that controls receive keyboard events and appear correctly to screen readers:

- Always use native HTML elements for buttons, form elements, and labels. Only use a DIV element as a container to hold native controls; never repurpose a DIV as an interactive UI element.
- Use the `label` element, `title` attribute, or `aria-label` attribute where appropriate, to provide information about a UI element.

### Position Custom Controls

Use the `slot` attribute to position custom controls, specifying the needed control position.
For information on these positions, see [Control Positioning](https://developers.google.com/maps/documentation/javascript/controls.md.txt#ControlPositioning)
above.

Each `ControlPosition` stores an `MVCArray` of the
controls displayed in that position. As a result, when controls are added or
removed from the position, the API will update the controls accordingly.

The following code creates a new custom control (its constructor is not
shown) and adds it to the map in the ` BLOCK_START_INLINE_END` position
(top-right in LTR context).

```javascript
// Create a DIV to attach the control UI to the Map.
const centerControlDiv = document.createElement("div");

// Create the control. This code calls a function that
// creates a new instance of a button control.
const centerControl = createCenterControl(map);

// Append the control to the DIV.
centerControlDiv.appendChild(centerControl);

// Push the control to the `BLOCK_START_INLINE_END` position.
innerMap.controls[google.maps.ControlPosition.BLOCK_START_INLINE_END].push(centerControlDiv);
```

To set position for a custom control declaratively, set the `slot` attribute in the
HTML:

```html
<gmp-map center="30.72851568848909, -81.54675994068873" zoom="12">
  <div slot="control-block-start-inline-end">
    <!-- Control HTML -->
  </div>
</gmp-map>
```

### A Custom Control Example

The following control is simple (though not particularly useful) and
combines the patterns shown above. This control responds to DOM
`'click'` events by centering the map at a certain default
location:

### TypeScript

```typescript
const chicago = { lat: 41.85, lng: -87.65 };

// Get the gmp-map element
const mapElement = document.querySelector('gmp-map')!;

async function init() {
    (await google.maps.importLibrary('maps')) as typeof google.maps;

    // Get the inner map.
    const innerMap = mapElement.innerMap;

    // Get the button element from the HTML
    const centerButton = document.getElementById(
        'btnCenterMap'
    ) as HTMLButtonElement;
    // Move the event listener here
    centerButton.addEventListener('click', () => {
        innerMap.setCenter(chicago);
    });
}

void init();
```

### JavaScript

```javascript
const chicago = { lat: 41.85, lng: -87.65 };

// Get the gmp-map element
const mapElement = document.querySelector('gmp-map');

async function init() {
    await google.maps.importLibrary('maps');

    // Get the inner map.
    const innerMap = mapElement.innerMap;

    // Get the button element from the HTML
    const centerButton = document.getElementById('btnCenterMap');
    // Move the event listener here
    centerButton.addEventListener('click', () => {
        innerMap.setCenter(chicago);
    });
}

void init();
```
[View example](https://developers.google.com/maps/documentation/javascript/examples/control-custom.md.txt)

### Try Sample

[JSFiddle.net](https://jsfiddle.net/gh/get/library/pure/googlemaps-samples/js-api-samples/tree/main/dist/samples/control-custom/jsfiddle)

### Add State to Controls

Controls may also store state. The following example is similar to that
shown before, but the control contains an additional "Set Home" button which
sets the control to exhibit a new home location. We do so by creating a
`home_` property within the control to store this state and
provide getters and setters for that state.

### TypeScript

```typescript
let innerMap: google.maps.Map;
const mapElement = document.querySelector('gmp-map')!;

let center: google.maps.LatLngLiteral | google.maps.LatLng = {
    lat: 41.85,
    lng: -87.65,
};

async function init() {
    await google.maps.importLibrary('maps');

    innerMap = mapElement.innerMap;

    // Get the button UI elements.
    const centerMapButton = document.getElementById(
        'btnCenterMap'
    ) as HTMLInputElement;
    const setCenterButton = document.getElementById(
        'btnSetCenter'
    ) as HTMLInputElement;

    // Set up the click event listener for the 'Center Map' button. Set the map
    // to the currently stored center.
    centerMapButton.addEventListener('click', () => {
        const currentCenter = center;
        innerMap.setCenter(currentCenter);
    });

    // Set up the click event listener for 'Set Center': Set the center of
    // the control to the current center of the map.
    setCenterButton.addEventListener('click', () => {
        const newCenter = innerMap.getCenter();

        if (newCenter) {
            center = newCenter;
        }
    });
}

void init();
```

### JavaScript

```javascript
let innerMap;
const mapElement = document.querySelector('gmp-map');

let center = {
    lat: 41.85,
    lng: -87.65,
};

async function init() {
    await google.maps.importLibrary('maps');

    innerMap = mapElement.innerMap;

    // Get the button UI elements.
    const centerMapButton = document.getElementById('btnCenterMap');
    const setCenterButton = document.getElementById('btnSetCenter');

    // Set up the click event listener for the 'Center Map' button. Set the map
    // to the currently stored center.
    centerMapButton.addEventListener('click', () => {
        const currentCenter = center;
        innerMap.setCenter(currentCenter);
    });

    // Set up the click event listener for 'Set Center': Set the center of
    // the control to the current center of the map.
    setCenterButton.addEventListener('click', () => {
        const newCenter = innerMap.getCenter();

        if (newCenter) {
            center = newCenter;
        }
    });
}

void init();
```
[View example](https://developers.google.com/maps/documentation/javascript/examples/control-custom-state.md.txt)

### Try Sample

[JSFiddle.net](https://jsfiddle.net/gh/get/library/pure/googlemaps-samples/js-api-samples/tree/main/dist/samples/control-custom-state/jsfiddle)