Get started

Show a map

Show a map with controls

MapView draws the map. MapControls is the floating stack over it, and each built-in control is a flag you switch on. The Props tab lists every component and prop.

Render the map

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

const client = new Mountaya({ credential: { publishableKey: "pk_your_publishable_key" } });

export const Map = () => (
  <div style={{ height: 480 }}>
    <MapView center={[6.8712, 45.9231]} client={client} zoom={12}>
      <MapControls compass geolocate layers preferences zoom />
    </MapView>
  </div>
);

MapView fills its container, so give the container a height. Packages and the stylesheet are on Install.

Hear what the map refuses

A map's own requests reject no call of yours. Pass onAccessRequired and onError to hear about them:

<MapView
  client={client}
  onAccessRequired={({ status, url }) => console.warn(`The API refused ${url} with a ${status}.`)}
  onError={(error) => console.error(error)}
  onReady={setMap}
/>;

Add the control stack

Each built-in control is a flag, off by default. Children join the same stack, spacing and focus order.

<MapControls
  compass
  disabled={agentIsDriving}
  geolocate
  layers
  onClosePanel={() => setPanel(null)}
  onTogglePanel={(next) => setPanel((current) => (current === next ? null : next))}
  openPanel={panel}
  pois
  position="top-right"
  preferences
  zoom
/>

Leave openPanel unset and the stack opens its own panels. Set it to control them yourself.

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
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.

Put SearchControl in the stack's children. It takes the stack's corner and searches through the map's client, which needs a session token.

<MapControls position="top-left">
  <SearchControl
    limit={5}
    onQueryChange={setQuery}
    onSelect={focusResult}
    query={query}
    targets={["itineraries"]}
  />
</MapControls>;

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 zoom>
  <MapControls.StreetView
    bounds={streetViewBounds}
    googleMapsApiKey={googleMapsApiKey}
    minZoom={14}
    onOpen={(lngLat) => openPanorama(lngLat)}
    onUnavailable={notify}
  />
</MapControls>;

Choose terrains, points of interest and units

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

<MapControls
  layers
  layersPanel={{
    isLocked: (slug) => !viewerPlan.terrains.includes(slug),
    onLockedClick: openUpgrade,
  }}
  poiPanel={{ minZoom: 12, showAllToggle: true }}
  pois
  preferences
  preferencesPanel={{ onPreferencesChange: setPreferences, preferences }}
  zoom
/>

Mount a panel on its own when it belongs in a sidebar or a settings drawer.

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 compass zoom>
  <DownloadControl onClick={() => setExportOpen(true)} />
  <DirectionsControl onClick={() => setDirectionsOpen(true)} />
</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 compass zoom>
  <ControlButton glyph="download" label="Print" onClick={() => window.print()} />
</MapControls>;

Icons, strings and colours come from the provider: see Customize the elements. The camera and provider hooks a control reads are on Hooks.

Next steps

Last updated