MapboxTooltip

View source
悬浮提示,鼠标移到目标图层要素时跟随显示,slot 暴露命中要素。

简介

MapboxTooltip 是 Popup 的 hover 模式:鼠标悬浮到 layerId 指定图层的要素上时跟随显示、移出即消失。默认插槽暴露命中要素(feature),可渲染其属性;按 layerId 委托到地图级监听,因此目标图层晚于本组件挂载也能命中。

用法

悬浮圆点图层显示要素信息,slot 取 feature.properties

<script setup lang="ts">
import type { FeatureCollection } from 'geojson'

const data: FeatureCollection = {
  type: 'FeatureCollection',
  features: [
    {
      type: 'Feature',
      properties: { name: '天安门', type: '地标' },
      geometry: { type: 'Point', coordinates: [116.397, 39.908] }
    },
    {
      type: 'Feature',
      properties: { name: '国贸', type: '商圈' },
      geometry: { type: 'Point', coordinates: [116.461, 39.909] }
    },
    {
      type: 'Feature',
      properties: { name: '中关村', type: '科技园' },
      geometry: { type: 'Point', coordinates: [116.316, 39.983] }
    }
  ]
}
</script>

<template>
  <div class="h-115 w-full overflow-hidden rounded-(--ui-radius) border border-default">
    <MapboxMap
      :options="{ style: 'mapbox://styles/mapbox/light-v11', center: [116.39, 39.93], zoom: 11 }"
    >
      <MapboxLayer
        layer-id="poi"
        type="circle"
        :source="{ type: 'geojson', data }"
        :paint="{
          'circle-radius': 9,
          'circle-color': '#f43f5e',
          'circle-stroke-width': 2,
          'circle-stroke-color': '#fff'
        }"
      />
      <!-- 悬浮 poi 图层要素时跟随显示,slot 暴露命中要素 -->
      <MapboxTooltip layer-id="poi">
        <template #default="{ feature }">
          <div v-if="feature" class="px-1 text-sm">
            <span class="font-semibold">{{ feature.properties?.name }}</span>
            <span class="text-muted"> · {{ feature.properties?.type }}</span>
          </div>
        </template>
      </MapboxTooltip>
    </MapboxMap>
  </div>
</template>

API

Props

Prop Default Type
layerIdstring

目标图层 id

optionsmapboxgl.PopupOptions

Popup 选项(closeButton/closeOnClick 由组件接管)

cursor'pointer'string

悬浮时鼠标指针样式

Slots

Slot Type
default{ feature: mapboxgl.GeoJSONFeature; }

Expose

通过 useTemplateRef 访问组件实例。

NameType
hovered() => GeoJSONFeature | undefined

当前悬浮命中的要素

Changelog

No recent changes
Copyright © 2026 - 2026 YiXuan - MIT License