Get started
TypeScript SDK
Show a map

Controls and panels

Controls and panels

MapControls is the floating stack over the map. Zoom, compass, layers and preferences show by default. The Props tab lists every control and prop.

Add the control stack

Place the stack inside MapView, and switch controls on or off by name:

import { MapControls, MapView } from "@mountaya/elements-react";

<MapView client={client}>
  <MapControls geolocate pois position="top-left" preferences={false} />
</MapView>;

Children join the same stack, spacing and focus order. Leave openPanel unset and the stack opens its own panels. Set it to control them yourself:

<MapControls
  disabled={agentIsDriving}
  onClosePanel={() => setPanel(null)}
  onTogglePanel={(next) => setPanel((current) => (current === next ? null : next))}
  openPanel={panel}
/>;

Handlers replace the default

Left unset, a control drives the map itself. Set an on* handler and yours runs instead:

<ZoomControl />;                        // zooms the map
<ZoomControl onZoomIn={bus.zoomIn} />;  // calls you, and does not zoom

DownloadControl and DirectionsControl require their onClick, because only your app knows what to open:

<MapControls>
  <DownloadControl onClick={() => setExportOpen(true)} />
  <DirectionsControl onClick={() => setDirectionsOpen(true)} />
</MapControls>

Pick the control for the job

You want toReach for
Zoom in and outzoom, or ZoomControl
Reset the bearing to northcompass, or CompassControl
Center on the reader's positiongeolocate, or GeolocateControl
Switch terrain and overlayslayers, which opens LayersPanel
Toggle what the POI overlay drawspois, which opens PoiPanel
Change units and map languagepreferences, which opens PreferencesPanel
Open a floating panel of your ownPanelControl
Search places, itineraries and zonesSearchControl, on search
Look around a streetStreetViewControl
Export what the map showsDownloadControl
Open directions for what the map showsDirectionsControl
Add a button that matches the built-insControlButton

CompassRose is the needle CompassControl draws, for a compass of your own. StudioControls belongs to the studio chrome.

Look around with Street View

StreetViewControl is a handle the reader drags onto the map. The imagery is Google's, so it needs your Google Maps API key and the @googlemaps/js-api-loader package.

<MapControls>
  <MapControls.StreetView
    bounds={streetViewBounds}
    googleMapsApiKey={googleMapsApiKey}
    minZoom={14}
    onOpen={(lngLat) => openPanorama(lngLat)}
    onUnavailable={notify}
  />
</MapControls>;

Build a control of your own

Five primitives make a control indistinguishable from the built-ins: ControlButton, ControlGroup, ControlTooltip, Glyph and OptionRow.

import { ControlButton, MapControls } from "@mountaya/elements-react";

<MapControls>
  <ControlButton glyph="download" label="Print" onClick={() => window.print()} />
</MapControls>;

Icons, strings and colours come from the provider: see Customize the elements.

Choose terrains, points of interest and units

Three panels open from the stack. Each takes its options through a prop named after it:

import { useState } from "react";
import { defaultUnits, type UnitFormatOptions } from "@mountaya/elements/profile";
import { MapControls } from "@mountaya/elements-react";

// One row for water, drawn by the drinking-water points of interest.
const water = [{ id: "water", keys: ["drinking-water"], label: "Water" }];

export const Controls = () => {
  const [units, setUnits] = useState<UnitFormatOptions>({ ...defaultUnits, length: "metric" });

  return (
    <MapControls
      layersPanel={{ thumbnail: (kind, slug) => `/layers/${kind}/${slug}.webp` }}
      poiPanel={{ groups: water, minZoom: 12 }}
      pois
      preferencesPanel={{ onPreferencesChange: setUnits, preferences: units }}
    />
  );
};

The points-of-interest panel offers one water row, and the units start metric. Mount a panel on its own when it belongs in a sidebar or a settings drawer.

Move through time

Weather overlays change with the hour. Draw the snow-depth overlay, and offersTime adds a time slider to the layers panel:

import { MapControls, MapView } from "@mountaya/elements-react";

<MapView client={client} onReady={(map) => void map.setOverlay("snow-depth")}>
  <MapControls layersPanel={{ offersTime: true }} />
</MapView>;

The map then draws the snow depth for the hour the reader picks. Back on now, it follows the clock again.

Next steps

Last updated