Code example
import { useMemo, useState } from 'react';
import { createGeoPoint, createMapCameraPosition } from '@mapconductor/js-sdk-core';
import { InfoBubble } from '@mapconductor/js-sdk-react';
import { MapLibreDesign, MapLibreMapView2D, useMapLibreViewState } from '@mapconductor/react-for-maplibre';
import { GeoJSONLayer, GeoJSONLayerState, type GeoJSONFeatureData } from '@mapconductor/react-geojson-layer';
// (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 [features, setFeatures] = useState<GeoJSONFeatureData[]>([]);
const [selected, setSelected] = useState<SelectedFeature | null>(null);
const layerState = useMemo(() => new GeoJSONLayerState({ id: 'railways' }), []);
// (3) Render the map and its overlays
<MapLibreMapView2D state={mapViewState} onMapClick={handleMapClick}>
<GeoJSONLayer state={layerState} features={features} />
{selected && (
<InfoBubble position={selected.position}>
<PropertyTable properties={selected.properties} />
</InfoBubble>
)}
</MapLibreMapView2D>
How the code works
Process clicks against GeoJSON features and show the selected feature's properties at the geographic click position.
Both the features and the selected feature live in React state, and layerState is a GeoJSONLayerState holding the railway data.
handleMapClick resolves which feature was hit and stores it, then InfoBubble anchors a PropertyTable of its properties at the clicked coordinate.