Routes 路线

在地图上渲染连接坐标点的线/路线。支持点击和悬停交互,可构建路线选择 UI。

基本用法

MapWithRoute.tsx
123456789101112131415161718
import { Map, MapRoute } from class="tk-string">"@/components/ui/map";

const route = [
  [-74.006, 40.7128], class=class="tk-string">"tk-comment">// 纽约
  [-73.9857, 40.7484], class=class="tk-string">"tk-comment">// 帝国大厦
  [-73.9851, 40.7589], class=class="tk-string">"tk-comment">// 时代广场
  [-73.9724, 40.7831], class=class="tk-string">"tk-comment">// 中央公园
];

export function MapWithRoute() {
  return (
    <div className=class="tk-string">"h-[320px] overflow-hidden rounded-lg">
      <Map center={[-73.99, 40.75]} zoom={12}>
        <MapRoute coordinates={route} />
      </Map>
    </div>
  );
}

自定义样式

自定义路线颜色、宽度和透明度:

123456
<MapRoute
  coordinates={route}
  color=class="tk-string">"#ef4444"
  width={4}
  opacity={0.9}
/>

虚线

使用 dashArray 创建虚线:

123456
<MapRoute
  coordinates={route}
  color=class="tk-string">"#f59e0b"
  width={3}
  dashArray={[2, 2]}
/>

交互路线

启用鼠标事件以构建路线选择 UI:

InteractiveRoute.tsx
12345678910111213141516171819202122
import { Map, MapRoute } from class="tk-string">"@/components/ui/map";
import { useState } from class="tk-string">"react";

export function InteractiveRoute() {
  const [hovered, setHovered] = useState(false);

  return (
    <div className=class="tk-string">"h-[320px] overflow-hidden rounded-lg">
      <Map center={[-73.99, 40.75]} zoom={12}>
        <MapRoute
          coordinates={route}
          color={hovered ? class="tk-string">"#22d3ee" : class="tk-string">"#4285F4"}
          width={hovered ? 5 : 3}
          interactive
          onMouseEnter={() => setHovered(true)}
          onMouseLeave={() => setHovered(false)}
          onClick={() => console.log(class="tk-string">"路线被点击")}
        />
      </Map>
    </div>
  );
}

属性

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

下一步

弧线

MapArc 组件