Code example
import { useMemo, useState } from 'react';
import {
createGeoPoint,
createMapCameraPosition,
createMarkerState,
createPolylineState,
type GeoPoint,
} from '@mapconductor/js-sdk-core';
import { Markers, Polyline } from '@mapconductor/js-sdk-react';
import { MapLibreDesign, MapLibreMapView2D, useMapLibreViewState } from '@mapconductor/react-for-maplibre';
// (1) Create the map
const initialCamera = createMapCameraPosition({
position: createGeoPoint({ latitude: 35.6812, longitude: 139.7671 }),
zoom: 12,
});
const mapViewState = useMapLibreViewState({
mapDesignType: MapLibreDesign.OsmBrightJa,
cameraPosition: initialCamera,
});
// (2) Prepare the data and interaction state
const [points, setPoints] = useState<GeoPoint[]>(initialPoints);
const polylineState = useMemo(() => createPolylineState({
id: 'route', points, strokeColor: '#ef4444', strokeWidth: 4,
}), [points]);
const waypointMarkers = points.map((position, index) => createMarkerState({
id: `waypoint-${index}`, position, draggable: true,
}));
// (3) Render the map and its overlays
<MapLibreMapView2D state={mapViewState}>
<Polyline state={polylineState} />
<Markers states={waypointMarkers} />
</MapLibreMapView2D>
How the code works
Draw a route from geographic points and expose its vertices as draggable waypoint markers.
The points array is React state, and useMemo rebuilds the PolylineState — a red, 4-pixel stroke — whenever those points change.
Every point is also turned into a draggable waypoint marker, so the route can be reshaped directly on the map.