Skip to content

API Reference ​

Complete reference for all map components and their props.

Note: This library is built on top of MapLibre GL JS. Most components extend the native MapLibre options. Refer to the MapLibre Map API for additional options not listed here.

Component Anatomy ​

All parts of the component that you can use and combine to build your map.

tsx
<Map>
  <MapMarker longitude={...} latitude={...}>
    <MarkerContent>
      <MarkerLabel />
    </MarkerContent>
    <MarkerPopup />
    <MarkerTooltip />
  </MapMarker>

  <MapPopup longitude={...} latitude={...} />
  <MapControls />
  <MapRoute coordinates={...} />
  <MapArc data={...} />
  <MapGeoJSON data={...} />
  <MapClusterLayer data={...} />
</Map>

Map ​

The root container component that initializes MapLibre GL and provides context to child components. Automatically handles theme switching between light and dark modes.

Extends MapOptions from MapLibre GL (excluding container and style).

PropTypeDefaultDescription
childrenReactNode—Child components (markers, popups, controls, routes).
classNamestring—Additional CSS classes for the map container.
theme"light" | "dark"—Theme for the map. If not provided, automatically detects from document class or system preference.
styles{ light?: string | StyleSpecification; dark?: string | StyleSpecification }—Custom map styles for light and dark themes. Overrides the default Carto base map tiles.
blankbooleanfalseUse a transparent, tile-less basemap instead of the default Carto street basemap. Ignored when an explicit styles prop is provided.
projectionProjectionSpecification—Map projection type. Use { type: "globe" } for 3D globe view.
viewportPartial<MapViewport>—Controlled viewport state. Can also be used alone for initial viewport.
onViewportChange(viewport: MapViewport) => void—Callback fired continuously as the viewport changes.
loadingbooleanfalseShow a loading indicator on the map.

useMap ​

A hook that provides access to the MapLibre map instance and loading state. Must be used within a Map component.

tsx
const { map, isLoaded } = useMap();

Returns map (MapLibre.Map) and isLoaded (boolean) telling you if the map is loaded and ready to use.

MapControls ​

Renders map control buttons (zoom, compass, locate, fullscreen). Must be used inside Map.

PropTypeDefaultDescription
position"top-left" | "top-right" | "bottom-left" | "bottom-right""bottom-right"Position of the controls on the map.
showZoombooleantrueShow zoom in/out buttons.
showCompassbooleanfalseShow compass button to reset bearing.
showLocatebooleanfalseShow locate button to find user's location.
showFullscreenbooleanfalseShow fullscreen toggle button.
classNamestring—Additional CSS classes for the controls container.
onLocate(coords: { longitude: number; latitude: number }) => void—Callback with user coordinates when located.

MapMarker ​

A container for marker-related components. Provides context for its children and handles marker positioning.

Extends MarkerOptions from MapLibre GL (excluding element).

PropTypeDescription
longitudenumberLongitude coordinate for marker position.
latitudenumberLatitude coordinate for marker position.
childrenReactNodeMarker subcomponents (MarkerContent, MarkerPopup, etc).
onClick(e: MouseEvent) => voidCallback when marker is clicked.
onMouseEnter(e: MouseEvent) => voidCallback when mouse enters marker.
onMouseLeave(e: MouseEvent) => voidCallback when mouse leaves marker.
onDragStart(lngLat: { lng, lat }) => voidCallback when marker drag starts (requires draggable: true).
onDrag(lngLat: { lng, lat }) => voidCallback during marker drag (requires draggable: true).
onDragEnd(lngLat: { lng, lat }) => voidCallback when marker drag ends (requires draggable: true).

MarkerContent ​

Renders the visual content of a marker. Must be used inside MapMarker. If no children provided, renders a default blue dot marker.

PropTypeDescription
childrenReactNodeCustom marker content. Defaults to a blue dot.
classNamestringAdditional CSS classes for the marker container.

MarkerPopup ​

Renders a popup attached to the marker that opens on click. Must be used inside MapMarker.

Extends PopupOptions from MapLibre GL (excluding className and closeButton).

PropTypeDefaultDescription
childrenReactNode—Popup content.
classNamestring—Additional CSS classes for the popup container.
closeButtonbooleanfalseShow a close button in the popup.

MarkerTooltip ​

Renders a tooltip that appears on hover. Must be used inside MapMarker.

Extends PopupOptions from MapLibre GL (excluding className, closeButton, and closeOnClick).

PropTypeDescription
childrenReactNodeTooltip content.
classNamestringAdditional CSS classes for the tooltip container.

MarkerLabel ​

Renders a text label above or below the marker. Must be used inside MarkerContent.

PropTypeDefaultDescription
childrenReactNode—Label text content.
classNamestring—Additional CSS classes for the label.
position"top" | "bottom""top"Position of the label relative to the marker.

MapPopup ​

A standalone popup component that can be placed anywhere on the map without a marker. Must be used inside Map.

Extends PopupOptions from MapLibre GL (excluding className and closeButton).

PropTypeDefaultDescription
longitudenumber—Longitude coordinate for popup position.
latitudenumber—Latitude coordinate for popup position.
onClose() => void—Callback when popup is closed.
childrenReactNode—Popup content.
classNamestring—Additional CSS classes for the popup container.
closeButtonbooleanfalseShow a close button in the popup.

MapRoute ​

Renders a line/route on the map connecting coordinate points. Must be used inside Map. Supports click and hover interactions for building route selection UIs.

PropTypeDefaultDescription
idstringauto-generatedOptional unique identifier for the route layer.
coordinates[number, number][]—Array of [longitude, latitude] coordinate pairs.
colorstring"#4285F4"Line color (CSS color value).
widthnumber3Line width in pixels.
opacitynumber0.8Line opacity (0 to 1).
dashArray[number, number]—Dash pattern [dash length, gap length] for dashed lines.
onClick() => void—Callback when the route line is clicked.
onMouseEnter() => void—Callback when mouse enters the route line.
onMouseLeave() => void—Callback when mouse leaves the route line.
interactivebooleanfalseRespond to mouse events (hover, cursor, callbacks).

MapArc ​

Renders curved lines between coordinate pairs using a quadratic Bézier in longitude/latitude space. Must be used inside Map. Supports click and hover interactions.

Built on a MapLibre line layer — the paint and layout props accept any field from LineLayerSpecification. Style per arc by passing a MapLibre expression as any paint value.

PropTypeDefaultDescription
dataMapArcDatum[]—Arcs to render. Each needs a unique id and from / to as [lng, lat].
idstringautoId prefix for the underlying source/layers.
curvaturenumber0.2How far the arc bows away from a straight line.
samplesnumber64Points per arc. Higher = smoother.
paintLineLayerSpecification['paint']{ "line-color": "#4285F4", "line-width": 2, "line-opacity": 0.85 }Paint props merged over defaults.
layoutLineLayerSpecification['layout']{ "line-join": "round", "line-cap": "round" }Layout props merged over defaults.
hoverPaintLineLayerSpecification['paint']—Paint overrides applied to the hovered arc via feature-state.
onClick(e: MapArcEvent) => void—Fired when an arc is clicked.
onHover(e: MapArcEvent | null) => void—Fired when the hovered arc changes.
interactivebooleantrueRespond to mouse events.
beforeIdstring—Insert the arc layers before this layer id.

MapGeoJSON ​

Renders arbitrary GeoJSON as fill + outline layers. Must be used inside Map — typically with the blank prop for choropleths and region/data maps. Accepts a FeatureCollection, Feature, Geometry, or a URL string to fetch from. Supports a generic type parameter for typed feature properties: MapGeoJSON<MyProperties>.

PropTypeDescription
dataFeatureCollection | Feature | Geometry | stringGeoJSON data or a URL string to fetch it from.
idstringId prefix for the underlying source/layers.
promoteIdstringFeature property to promote to the feature id. Required for hover feature-state.
fillPaintFillLayerSpecification['paint'] | falsePaint for the polygon fill layer. Pass false to omit the fill layer.
linePaintLineLayerSpecification['paint'] | falsePaint for the outline layer. Pass false to omit the outline layer.
fillHoverPaintFillLayerSpecification['paint']Paint merged onto the fill layer for the hovered feature. Requires promoteId.
onClick(e: MapGeoJSONEvent) => voidFired when a feature is clicked.
onHover(e: MapGeoJSONEvent | null) => voidFired when the hovered feature changes.
interactivebooleanRespond to mouse events (default false).
beforeIdstringInsert the layers before this layer id.

MapClusterLayer ​

Renders clustered point data using MapLibre GL's native clustering. Must be used inside Map. Supports a generic type parameter: MapClusterLayer<MyProperties>.

PropTypeDefaultDescription
datastring | GeoJSON.FeatureCollection—GeoJSON FeatureCollection data or URL to fetch GeoJSON from.
clusterMaxZoomnumber14Maximum zoom level to cluster points on.
clusterRadiusnumber50Radius of each cluster when clustering points (in pixels).
clusterColors[string, string, string]["#3b82f6", "#1d4ed8", "#1e3a8a"]Colors for cluster circles: [small, medium, large].
clusterThresholds[number, number][100, 750]Point count thresholds for color/size steps.
pointColorstring"#3b82f6"Color for unclustered individual points.
onPointClick(feature, coordinates) => void—Callback when an unclustered point is clicked.
onClusterClick(clusterId, coordinates, pointCount) => void—Callback when a cluster is clicked. If not provided, zooms into the cluster.

基于 mapcn 官方文档整理 · AI 辅助编撰