MapConductor React SDK Samples

Polygon | MapLibre | MapConductor React SDK

MapConductor React SDK Polygon sample using MapLibre.

This interactive example demonstrates the Polygon feature through the MapConductor abstraction on MapLibre.

Code example

import { useMemo, useState } from 'react';
import {
  createGeoPoint,
  createMapCameraPosition,
  createMarkerState,
  createPolygonState,
  type GeoPoint,
} from '@mapconductor/js-sdk-core';
import { Markers, Polygon } 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 [vertices, setVertices] = useState<GeoPoint[]>(initialVertices);
const polygonState = useMemo(() => createPolygonState({
  id: 'area',
  points: vertices,
  fillColor: 'rgba(37, 99, 235, 0.3)',
  strokeColor: '#2563eb',
}), [vertices]);
const vertexMarkers = vertices.map((position, index) => createMarkerState({
  id: `vertex-${index}`, position, draggable: true,
}));


// (3) Render the map and its overlays


<MapLibreMapView2D state={mapViewState}>
  <Polygon state={polygonState} />
  <Markers states={vertexMarkers} />
</MapLibreMapView2D>

How the code works

Render a filled polygon and use markers to make each vertex visible and interactive.

The vertices array is React state, and useMemo rebuilds the PolygonState — a translucent blue fill with a solid stroke — whenever a vertex moves.

Each vertex is drawn as a draggable marker, so the polygon outline can be edited point by point.