API 参考

所有地图组件及其属性的完整参考。

💡 注意:此库基于MapLibre GL JS构建。大多数组件扩展了原生 MapLibre 选项。未列出的选项请参考MapLibre Map API

组件结构

所有你可以使用和组合来构建地图的组件部分:

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

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

Map

根容器组件,初始化 MapLibre GL 并为子组件提供上下文。自动处理浅色和深色模式之间的主题切换。

继承 MapOptions(排除 containerstyle)。

属性类型默认值描述
childrenReactNode子组件
classNamestring容器额外 CSS 类名
theme"light" | "dark"自动检测地图主题
styles{ light?: string | StyleSpecification; dark?: string | StyleSpecification }自定义地图样式
blankbooleanfalse透明无瓦片底图
projectionProjectionSpecification投影类型,如 3D 地球
viewportPartial<MapViewport>受控视口状态
onViewportChange(viewport: MapViewport) => void视口变化回调
loadingbooleanfalse显示加载指示器

useMap

提供访问 MapLibre 地图实例和加载状态的 hook。必须在 Map 组件内使用。

1
const { map, isLoaded } = useMap();
  • map — MapLibre.Map 实例
  • isLoaded — 布尔值,表示地图是否已加载就绪

MapControls

渲染地图控制按钮(缩放、指北针、定位、全屏)。必须在 Map 内部使用。

属性类型默认值描述
position"top-left" | "top-right" | "bottom-left" | "bottom-right""bottom-right"控件位置
showZoombooleantrue显示缩放按钮
showCompassbooleanfalse显示指北针
showLocatebooleanfalse显示定位按钮
showFullscreenbooleanfalse显示全屏按钮
classNamestring额外 CSS 类名
onLocate(coords: { longitude: number; latitude: number }) => void定位回调

MapMarker

标记相关组件的容器。为子组件提供上下文并处理标记定位。

继承 MarkerOptions(排除 element)。

属性类型默认值描述
longitudenumber经度
latitudenumber纬度
childrenReactNode标记子组件
onClick(e: MouseEvent) => void点击回调
onMouseEnter(e: MouseEvent) => void鼠标进入回调
onMouseLeave(e: MouseEvent) => void鼠标离开回调
onDragStart(lngLat: {lng, lat}) => void拖拽开始回调
onDrag(lngLat: {lng, lat}) => void拖拽中回调
onDragEnd(lngLat: {lng, lat}) => void拖拽结束回调

MarkerContent

渲染标记的视觉内容。必须在 MapMarker 内使用。无 children 时渲染默认蓝色圆点。

属性类型描述
childrenReactNode自定义标记内容,默认蓝色圆点
classNamestring额外 CSS 类名

MarkerPopup

渲染附加到标记的弹窗,点击时打开。必须在 MapMarker 内使用。

继承 PopupOptions(排除 classNamecloseButton)。

属性类型默认值描述
childrenReactNode弹窗内容
classNamestring额外 CSS 类名
closeButtonbooleanfalse显示关闭按钮

MarkerTooltip

渲染悬停时显示的提示。必须在 MapMarker 内使用。

属性类型描述
childrenReactNode提示内容
classNamestring额外 CSS 类名

MarkerLabel

渲染标记上方或下方的文字标签。必须在 MarkerContent 内使用。

属性类型默认值描述
childrenReactNode标签文字
classNamestring额外 CSS 类名
position"top" | "bottom""top"标签相对位置

MapPopup

独立弹窗组件,可放在地图任意位置,无需标记。必须在 Map 内使用。

继承 PopupOptions(排除 classNamecloseButton)。

属性类型默认值描述
longitudenumber经度
latitudenumber纬度
onClose() => void关闭回调
childrenReactNode弹窗内容
classNamestring额外 CSS 类名
closeButtonbooleanfalse显示关闭按钮

MapRoute

渲染连接坐标点的线/路线。必须在 Map 内使用。支持点击和悬停交互。

属性类型默认值描述
idstring自动生成路线图层唯一标识
coordinates[number, number][]坐标对数组
colorstring"#4285F4"线条颜色
widthnumber3线条宽度(像素)
opacitynumber0.8线条透明度
dashArray[number, number]虚线模式
onClick() => void点击回调
onMouseEnter() => void鼠标进入回调
onMouseLeave() => void鼠标离开回调
interactivebooleanfalse响应鼠标事件

MapArc

使用二次贝塞尔曲线渲染坐标对之间的弯曲弧线。必须在 Map 内使用。

基于 MapLibre 线图层。支持 MapLibre 表达式实现逐要素样式。

属性类型默认值描述
dataMapArcDatum[]弧线数据数组
idstring自动源/图层 id 前缀
curvaturenumber0.2弯曲程度。0 为直线
samplesnumber64每条弧线采样点数
paintLineLayerSpecification['paint']默认蓝线绘制属性,支持表达式
layoutLineLayerSpecification['layout']默认圆角布局属性
hoverPaintLineLayerSpecification['paint']悬停绘制覆盖
onClick(e: MapArcEvent) => void点击事件
onHover(e: MapArcEvent | null) => void悬停事件
interactivebooleantrue响应鼠标事件
beforeIdstring在此图层前插入

MapGeoJSON

将任意 GeoJSON 渲染为填充 + 轮廓图层。必须在 Map 内使用——通常配合 blank 属性。

属性类型默认值描述
dataFeatureCollection | Feature | Geometry | stringGeoJSON 数据或 URL
idstring自动源/图层 id 前缀
promoteIdstring提升为要素 id 的属性名
fillPaintFillLayerSpecification['paint'] | false主题感知填充绘制属性。false 省略
linePaintLineLayerSpecification['paint'] | false主题感知轮廓绘制属性。false 省略
fillHoverPaintFillLayerSpecification['paint']悬停填充覆盖。需 promoteId
onClick(e: MapGeoJSONEvent) => void点击事件
onHover(e: MapGeoJSONEvent | null) => void悬停事件
interactivebooleanfalse响应鼠标事件
beforeIdstring在此图层前插入

MapClusterLayer

使用 MapLibre GL 原生聚类渲染聚类点数据。自动将附近点分组为聚类,点击展开。必须在 Map 内使用。

属性类型默认值描述
datastring | GeoJSON.FeatureCollectionGeoJSON 数据或 URL
clusterMaxZoomnumber14聚类最大缩放级别
clusterRadiusnumber50聚类半径(像素)
clusterColors[string, string, string]绿/黄/红聚类颜色 [小, 中, 大]
clusterThresholds[number, number][100, 750]颜色/大小阈值 [中, 大]
pointColorstring"#3b82f6"未聚类点颜色
onPointClick(feature, coordinates) => void单点点击回调
onClusterClick(clusterId, coordinates, pointCount) => void放大展开聚类点击回调

继续探索

高级用法

useMap Hook 和自定义图层