Get started
Show a map

Edit an itinerary on the map

Edit an itinerary on the map

EditorMapView draws the map an itinerary is edited on. It hands you a MapClient, the object every drawing command goes through.

Install the drawing engines

The editor needs three more packages beside the map's install line:

npm install terra-draw terra-draw-maplibre-gl-adapter @maplibre/maplibre-gl-directions

Mount the editor map

import { EditorMapView } from "@mountaya/elements-react/editor";

<EditorMapView
  ariaLabel="Route editor"
  client={client}
  onClientReady={holdMapClient}
  onMapReady={holdMap}
/>;

onClientReady is the one required prop: it hands you the MapClient. Use ViewerMapView, with the same props, for a map that shows an itinerary nobody may change.

Open an edit session

Arm an edit, listen to the drafts it produces, and end it when you are done:

const stopListening = mapClient.onSegmentDraftChange((draft) => {
  if (draft.phase === "settled") {
    keepVias(draft.vias);
  }
});

mapClient.beginSegmentEdit({ color: "#e9501e", featureId: segmentId, mode: "linestring" });

// Later, when the edit ends.
mapClient.endSegmentEdit();
stopListening();

A draft arrives live on every frame of a drag, and settled once the drag ends. Record one undo step per settled draft.

MapClient commandDraws
beginSegmentEditA segment's line, freehand or routed
beginWaypointEditA waypoint, dropped or picked off a POI
beginZoneEditA zone: circle, sector, sensor, polygon or freehand
beginBoundaryEditA rectangular boundary box

Route a segment as it is drawn

A routed segment asks a SegmentRouteFn for its line. The studio passes one that calls the Data API's directions; on your own EditorMapView, pass the router you have.

Keep a studio in step

useStudioMapBindings(client, options?) from @mountaya/studio-react/map binds the map to a studio, with every hook below in the right order. Call it once per map.

Record a video

video takes a recorder factory. Pass nothing and no recorder is built.

<EditorMapView client={client} onClientReady={holdMapClient} video={attachRecorder} />;

Test against a MapClient double

createMapClientStub and createMapClientSpy from @mountaya/elements/testing stand in for the map client in a test suite. See Testing.

Next steps

Last updated