Control Slots

This example shows how all of the logical position slots appear on the map. The default controls are left in place to illustrate how the slots and controls behave together.

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

Read the documentation.

TypeScript

/**
 * Creates a series of custom controls to demonstrate positioning
 * of controls within a map.
 */

/**
 * MakeControl adds a control to the map.
 * This constructor takes the controlDIV name and label text as arguments.
 */
async function MakeControl(controlDiv: HTMLElement, label: string) {
    // Set up the control border.
    const controlUI = document.createElement('div');

    controlUI.style.backgroundColor = '#fff';
    controlUI.style.padding = '3px';
    controlUI.style.cursor = 'pointer';
    controlUI.style.textAlign = 'center';
    controlUI.title = 'Click to toggle RTL/LTR';
    controlUI.className = 'controlUI';
    controlDiv.appendChild(controlUI);

    // Set up the inner control.
    const controlText = document.createElement('div');
    controlText.style.fontSize = '12px';
    controlText.innerHTML = label;
    controlText.className = 'controlText';
    controlUI.appendChild(controlText);
}

async function initMap() {
    //  Request the needed libraries.
    await google.maps.importLibrary('maps');

    const mapElement = document.querySelector(
        'gmp-map'
    ) as google.maps.MapElement;

    const innerMap = mapElement.innerMap;

    const positions: (keyof typeof google.maps.ControlPosition)[] = [
        'BLOCK_START_INLINE_START',
        'INLINE_START_BLOCK_START',
        'BLOCK_START_INLINE_CENTER',
        'BLOCK_START_INLINE_END',
        'INLINE_END_BLOCK_START',
        'INLINE_START_BLOCK_CENTER',
        'INLINE_END_BLOCK_CENTER',
        'BLOCK_END_INLINE_START',
        'INLINE_START_BLOCK_END',
        'BLOCK_END_INLINE_CENTER',
        'BLOCK_END_INLINE_END',
        'INLINE_END_BLOCK_END',
    ];

    positions.forEach((position) => {
        const divName = document.createElement('div');
        const controlPosition = google.maps.ControlPosition[position];

        MakeControl(divName, position);
        divName.addEventListener('click', toggleRTL);
        innerMap.controls[controlPosition].push(divName);
    });
}

/**
 * Toggles the 'dir' attribute on the html element between 'ltr' and 'rtl'.
 */
async function toggleRTL() {
    const html = document.documentElement;
    if (html.dir === 'rtl') {
        html.dir = 'ltr';
    } else {
        html.dir = 'rtl';
    }
}

initMap();

JavaScript

/**
 * Creates a series of custom controls to demonstrate positioning
 * of controls within a map.
 */
/**
 * MakeControl adds a control to the map.
 * This constructor takes the controlDIV name and label text as arguments.
 */
async function MakeControl(controlDiv, label) {
    // Set up the control border.
    const controlUI = document.createElement('div');
    controlUI.style.backgroundColor = '#fff';
    controlUI.style.padding = '3px';
    controlUI.style.cursor = 'pointer';
    controlUI.style.textAlign = 'center';
    controlUI.title = 'Click to toggle RTL/LTR';
    controlUI.className = 'controlUI';
    controlDiv.appendChild(controlUI);
    // Set up the inner control.
    const controlText = document.createElement('div');
    controlText.style.fontSize = '12px';
    controlText.innerHTML = label;
    controlText.className = 'controlText';
    controlUI.appendChild(controlText);
}
async function initMap() {
    //  Request the needed libraries.
    await google.maps.importLibrary('maps');
    const mapElement = document.querySelector('gmp-map');
    const innerMap = mapElement.innerMap;
    const positions = [
        'BLOCK_START_INLINE_START',
        'INLINE_START_BLOCK_START',
        'BLOCK_START_INLINE_CENTER',
        'BLOCK_START_INLINE_END',
        'INLINE_END_BLOCK_START',
        'INLINE_START_BLOCK_CENTER',
        'INLINE_END_BLOCK_CENTER',
        'BLOCK_END_INLINE_START',
        'INLINE_START_BLOCK_END',
        'BLOCK_END_INLINE_CENTER',
        'BLOCK_END_INLINE_END',
        'INLINE_END_BLOCK_END',
    ];
    positions.forEach((position) => {
        const divName = document.createElement('div');
        const controlPosition = google.maps.ControlPosition[position];
        MakeControl(divName, position);
        divName.addEventListener('click', toggleRTL);
        innerMap.controls[controlPosition].push(divName);
    });
}
/**
 * Toggles the 'dir' attribute on the html element between 'ltr' and 'rtl'.
 */
async function toggleRTL() {
    const html = document.documentElement;
    if (html.dir === 'rtl') {
        html.dir = 'ltr';
    }
    else {
        html.dir = 'rtl';
    }
}
initMap();

CSS

/* 
 * Always set the map height explicitly to define the size of the div element
 * that contains the map. 
 */
gmp-map {
    height: 100%;
}

/* 
 * Optional: Makes the sample page fill the window. 
 */
html,
body {
    height: 100%;
    margin: 0;
    padding: 0;
}

HTML

<html>
    <head>
        <title>Control Positioning Labels</title>

        <link rel="stylesheet" type="text/css" href="./style.css" />
        <script type="module" src="./index.js"></script>
        <!-- prettier-ignore -->
        <script>(g=>{var h,a,k,p="The Google Maps JavaScript API",c="google",l="importLibrary",q="__ib__",m=document,b=window;b=b[c]||(b[c]={});var d=b.maps||(b.maps={}),r=new Set,e=new URLSearchParams,u=()=>h||(h=new Promise(async(f,n)=>{await (a=m.createElement("script"));e.set("libraries",[...r]+"");for(k in g)e.set(k.replace(/[A-Z]/g,t=>"_"+t[0].toLowerCase()),g[k]);e.set("callback",c+".maps."+q);a.src=`https://maps.${c}apis.com/maps/api/js?`+e;d[q]=f;a.onerror=()=>h=n(Error(p+" could not load."));a.nonce=m.querySelector("script[nonce]")?.nonce||"";m.head.append(a)}));d[l]?console.warn(p+" only loads once. Ignoring:",g):d[l]=(f,...n)=>r.add(f)&&u().then(()=>d[l](f,...n))})
        ({key: "AIzaSyA6myHzS10YXdcazAFalmXvDkrYCp5cLc8", v: "weekly"});</script>
    </head>
    <body>
        <gmp-map center="30.72851568848909, -81.54675994068873" zoom="12">
        </gmp-map>
    </body>
</html>

Try Sample

Clone Sample

Git and Node.js are required to run this sample locally. Follow these instructions to install Node.js and NPM. The following commands clone, install dependencies and start the sample application.

  git clone https://github.com/googlemaps-samples/js-api-samples.git
  cd samples/control-positioning-labels
  npm i
  npm start