Code example
import { useMemo, useState } from 'react';
import {
createGeoPoint,
createMapCameraPosition,
createMarkerState,
type MarkerState,
} from '@mapconductor/js-sdk-core';
import { InfoBubble, Markers } 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 [selected, setSelected] = useState<MarkerState | null>(null);
const markers = useMemo(() => markerData.map(item => createMarkerState({
id: item.id,
position: createGeoPoint(item.position),
icon: item.icon,
onClick: markerState => setSelected(markerState),
})), [markerData]);
// (3) Render the map and its overlays
<MapLibreMapView2D state={mapViewState}>
<Markers states={markers} />
{selected && <InfoBubble marker={selected}>{selected.extra}</InfoBubble>}
</MapLibreMapView2D>
How the code works
Compose markers with several icon sources and open a bubble when a marker is selected.
The markers array is memoized from markerData, and each createMarkerState carries its own icon plus an onClick that records the selected MarkerState.
The batched <Markers> renders every icon variant together, and the InfoBubble surfaces selected.extra only while a marker stays active.