Map Controls
UI components that provide user interactivity and control features for the map.
All built-in controls must adhere to the Control
Interface.
1. Control (Interface)
The base interface for any UI element that can be added to the map using
map.addControl().
This is an **interface**. Custom controls must implement these methods; built-in controls already do.
Interface Methods
Called when the control is added to the map.
Parameters:
| map | Map | The map instance the control is being added to. |
HTMLElement (The main container element for the control)
Called when the control is removed from the map. Should clean up DOM and event listeners.
void2. NavigationControl 🧭
Provides buttons for zooming and for resetting the map's rotation and pitch (compass).
Constructor
Creates a new NavigationControl instance.
Options (NavigationControlOptions)
The options below can be passed to the constructor to customize the control's appearance and functionality.
If true, the compass button (for resetting
rotation/pitch) is visible. **Default:** true.
If true, the zoom in (+) and zoom out (-) buttons are
visible. **Default:** true.
If true, the compass icon rotates and tilts to
visualize the map's current rotation and pitch. **Default:** false.
Usage Example
const nav = new NavigationControl({
showCompass: false, // Only show zoom buttons
visualizePitch: true
});
map.addControl(nav, 'top-right');
3. FullscreenControl 🖥️
Provides a button to toggle the map container into and out of fullscreen mode.
Constructor
Creates a new FullscreenControl instance.
Options (FullscreenControlOptions)
The DOM element that should be made fullscreen. **Default:** The map container element.
Usage Example
map.addControl(fsControl, 'top-left');
4. GeolocateControl 📍
Uses the browser's **Geolocation API** to find and display the user's current location on the map. It typically displays a pulsing dot for the location and a circle indicating the accuracy.
Constructor
Creates a new GeolocateControl instance.
Options (GeolocateControlOptions)
Configuration options to control the behavior of the geolocation tracking and visualization.
An object literal passed directly to the browser's Geolocation API's
getCurrentPosition and watchPosition calls. Common properties include:
enableHighAccuracy: **boolean** (Default: `false`)timeout: **number** (Maximum time in ms to wait for a position, Default: `6000`)maximumAge: **number** (Maximum age in ms of a cached position, Default: `0`)
An object literal passed to the map's fitBounds method when centering the map on
the user's location. This allows you to set options like **padding** or **maxZoom**.
*Example:* { maxZoom: 15, duration: 1000 }
If true, the map will continuously track the user's
position as it changes. **Default:** false.
If true, the user location dot and accuracy circle are
rendered on the map. **Default:** true.
If true, the accuracy circle is shown around the user
location dot. **Default:** true.
Events
The GeolocateControl emits events that allow you to react to tracking status and results.
Fired when a new **Geolocation Position** is successfully available.
The event's data contains the GeolocationPosition object.
Fired when the Geolocation API returns an error (e.g., permission denied, timeout).
Fired when the control is activated to start continuously tracking the user's location.
Fired when the control is deactivated and stops tracking the user's location.
Usage Example
const geoControl = new GeolocateControl({
positionOptions: { enableHighAccuracy: true },
trackUserLocation: true
});
map.addControl(geoControl, 'bottom-right');
// Listen for successful location updates
geoControl.on('geolocate', (e) => {
console.log('User located at:', e.coords.latitude, e.coords.longitude);
});
5. ScaleControl 📏
A **standard map control** that displays a scale bar indicating the current map distance relative to the screen distance.
Constructor
Creates a new ScaleControl instance.
Options (ScaleControlOptions)
The maximum width of the scale control in pixels. **Default:**
100.
The unit of the scale. Must be one of 'imperial',
'metric', or 'nautical'. **Default:** 'metric'.
Usage Example
map.addControl(scale, 'bottom-left');
6. TerrainControl ⛰️
Provides a toggle button to enable or disable 3D terrain on the map, assuming a suitable **Raster DEM source** is configured on the map's style.
This control requires the map to have a **Terrain Source** added to its style to function.
Constructor
Creates a new TerrainControl instance.
Options (TerrainControlOptions)
The ID of the terrain source that should be toggled by the control. **Required.**
The vertical exaggeration factor to apply when enabling terrain.
**Default:** 1.0.
Usage Example
Assumes a terrain source with ID `'gts-terrain'` is available in the map's style.
// 2. Add the control to toggle it on/off
const terrain = new TerrainControl({
sourceId: 'gts-terrain',
exaggeration: 1.5
});
map.addControl(terrain, 'top-right');
7. AttributionControl (Map Option)
Displays the map's required attribution and copyright information (e.g., OpenStreetMap, data
providers).
The control is typically managed via the main MapOptions.
Management
The AttributionControl is added by default when initializing the map. Its visibility is controlled
by the MapOptions properties:
If false, the AttributionControl is not added to the
map. **Default:** true.
Manual Control
If attributionControl is set to false during Map initialization, you can
manually add
an AttributionControl instance, though it's rarely necessary.
const map = new Map({ target: 'map', attributionControl: false, ... });
// Manually add the control later (optional)
const attribution = new AttributionControl({ compact: true });
map.addControl(attribution, 'bottom-left');