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

Flutter中Google Map无法添加多个标记 仅显示最后一个标记问题求助

问题根因

  • 第一优先级排查:_navigationList的首元素和尾元素的userId是否一致。因为Set<Marker>会根据MarkerId判断元素是否重复,若两个标记的markerId完全相同,后添加的标记会直接覆盖先添加的,最终Set里只会保留最后一个添加的标记。你可以先打印两个markerId的取值确认是否重复。
  • 第二优先级排查:坐标重叠问题。若两个标记的经纬度完全一致,后添加的标记会渲染在先添加的标记上方,视觉上只会看到最后一个标记,你可以分别打印两个标记的经纬度取值确认。
  • 第三优先级排查:异步执行时序问题。你的Markers是异步函数,内部加载图标需要等待,你在createMap中调用Markers时没有加await,如果有其他逻辑也在修改_listmarkers,可能会出现时序冲突导致标记被覆盖。

修复方案

  1. 首先保证两个标记的markerId全局唯一,如果你无法保证userId不重复,可以直接用固定前缀拼接索引的方式生成唯一ID,比如首标记用const MarkerId("first_marker"),尾标记用const MarkerId("last_marker"),避免ID重复。
  2. 如果是异步时序问题,修改方法定义和调用逻辑,确保标记添加完成后再执行其他操作:
// 修改Markers方法返回Future
Future<void> Markers() async {
  var firstPath = "img/first.png";
  final Uint8List firstIcon = await getBytesFromAsset(firstPath, 50);
  var lastPath = "img/last.png";
  final Uint8List lastIcon = await getBytesFromAsset(lastPath, 50);
  _listmarkers = Set<Marker>();
  _listmarkers.add(Marker(
    markerId: const MarkerId("first_marker"),
    position: LatLng(_navigationList.first.latitude,
        _navigationList.first.longitude),
    icon: BitmapDescriptor.fromBytes(firstIcon),
  ));
  _listmarkers.add(Marker(
    markerId: const MarkerId("last_marker"),
    position: LatLng(_navigationList.last.latitude,
        _navigationList.last.longitude),
    icon: BitmapDescriptor.fromBytes(lastIcon),
  ));
  setState(() {});
}

// 调用时添加await
void createMap() async {
  if (_navigationList.isNotEmpty) {
    _postsController.add(1);
    LatLngBounds bound = boundsFromLatLngList(_navigationList);
    // 等待标记加载完成
    await Markers();
  }
}
  1. 如果确认是坐标重叠导致的,可以调整其中一个标记的锚点,或者添加信息窗做区分,避免视觉上的覆盖。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 23:27:02