Skip to content

覆盖物 ​

本篇覆盖丰图地图 HarmonyOS SDK 提供的地图覆盖物类型及其用法。所有覆盖物均通过 map.addXxx(options, callback?) 添加,回调返回对应对象用于后续属性更新。

Marker 点标记 ​

功能说明 ​

点标记,支持六种图标来源(URL / rawfile / base64 / PixelMap / $r 资源 / 默认气泡)、锚点、拖拽、旋转、zIndex、批量添加。

关键代码 ​

ts
import { FTMapMarkerOptions, FTMapBitmapDescriptorFactory,
         FTMapCoordinate } from 'ftmap-openplatform';

// 基础
map.addMarker(new FTMapMarkerOptions(coord)
  .title('北京').snippet('首都').draggable(true).anchor(0.5, 1.0),
  (marker) => {
    marker.showInfoWindow();
    marker.setPosition(newCoord);   // InfoWindow 联动跟随
    marker.setVisible(false);       // InfoWindow 联动隐藏
  });

// 图标来源
.icon(FTMapBitmapDescriptorFactory.fromUrl('https://cdn.example.com/pin.png'))          // ① 远程
.icon(FTMapBitmapDescriptorFactory.fromAsset('demo/marker.png'))                        // ② rawfile
.icon(FTMapBitmapDescriptorFactory.fromBase64('iVBOR...'))                              // ③ base64
const icon4 = await FTMapBitmapDescriptorFactory.fromPixelMap(pm);                      // ④ PixelMap
const icon5 = await FTMapBitmapDescriptorFactory
  .fromMediaResource(ctx, $r('app.media.my_pin').id);                                  // ⑤ $r

// 批量
map.addMarkers([opts1, opts2], (markers) => { /* 全部就绪 */ });

要点 ​

  • rawfile 路径含 / 为工程资源(完整相对路径);纯名称为 SDK 内置目录(自动补 .png)。
  • 拖拽需 .draggable(true),拖拽中 InfoWindow 实时跟随。

map.clear() 清除全部覆盖物;单个覆盖物调用 xxx.remove()(destroy() 等价)。

Polyline 多段线 ​

ts
map.addPolyline(new FTMapPolylineOptions()
  .addPoint(a).addPoint(b)
  .width(5).color(0xFF0000FF).dotted(true), (polyline) => { ... });

Arc 弧线 ​

功能说明 ​

三点圆弧(起点 / 途经点 / 终点),途经点必经;三点共线退化为折线。

ts
map.addArc(new FTMapArcOptions()
  .points([start, via, end])       // 依次为起点/途经点/终点
  .strokeColor(0xFF3F85FF).strokeWidth(6), (arc) => { ... });

Polygon 多边形 / Circle 圆形 ​

ts
map.addPolygon(new FTMapPolygonOptions().points([a, b, c])
  .fillColor(0x8032639B).strokeColor(0xFFB3269964).strokeWidth(2), (p) => { ... });

map.addCircle(new FTMapCircleOptions().center(c).radius(5000)     // 半径单位: 米
  .fillColor(0x8000BBBB).strokeWidth(2), (c) => { ... });

Text 文字覆盖物 ​

ts
map.addText(new FTMapTextOptions(coord)
  .text('深圳市南山区')
  .fontSize(14).color(0xFF000000).bgColor(0xCC3F85FF).rotate(30), (t) => {
    t.setText('更新文字');
    t.setRotate(60);
  });

V3.2-202604301400