You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Flutter中为Yandex MapKit添加marker标记

Flutter Yandex Mapkit 标记(Placemark/Marker)添加教程

你理解的没错,Yandex Mapkit 中地图标记对应的就是 PlacemarkMapObject 实体,具体实现步骤如下:

  • 前置依赖确认:已在 pubspec.yaml 中引入 yandex_mapkit 依赖,且完成了 Yandex Mapkit 的密钥初始化、定位权限申请流程。
  • 第一步:准备自定义标记图标(如用系统默认图标可跳过本步骤)
    将需要作为标记的图片放入项目 assets 目录,并在 pubspec.yaml 的 assets 节点中完成资源声明,之后通过以下代码将图片转为 Yandex Mapkit 可识别的格式:
    import 'dart:ui' as ui;
    import 'package:flutter/services.dart';
    import 'package:yandex_mapkit/yandex_mapkit.dart';
    
    Future<ui.Image> loadMarkerAsset(String assetPath) async {
      final byteData = await rootBundle.load(assetPath);
      final imageCodec = await ui.instantiateImageCodec(byteData.buffer.asUint8List());
      final frameInfo = await imageCodec.getNextFrame();
      return frameInfo.image;
    }
    
  • 第二步:向地图添加 Placemark 标记
    拿到初始化完成的 YandexMapController 实例后,构造 PlacemarkMapObject 并加入地图的对象集合即可:
    late YandexMapController mapController;
    
    void addMarker(Point position, ui.Image? customIcon) {
      final placemark = PlacemarkMapObject(
        // 每个标记的mapId必须全局唯一
        mapId: MapObjectId('marker_${DateTime.now().millisecondsSinceEpoch}'),
        // 标记的经纬度坐标
        point: position,
        // 标记样式配置
        icon: PlacemarkIcon.single(PlacemarkIconStyle(
          image: customIcon != null ? BitmapDescriptor.fromImage(customIcon) : null,
          scale: 0.6,
          // 使用系统默认图标时可直接修改图标颜色
          // color: Colors.blue
        )),
        // 可选:配置标记点击事件
        onTap: (self, tapPoint) {
          // 自定义点击逻辑
        }
      );
    
      // 将标记渲染到地图上
      mapController.mapObjects.add(placemark);
    }
    

补充说明:如果需要移除已添加的标记,调用 mapController.mapObjects.remove(placemark) 传入对应标记实例即可;批量添加标记只需构造多个 PlacemarkMapObject 实例加入集合即可。

内容的提问来源于stack exchange,提问作者fasmof

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 05:45:00