【问题标题】:Cannot add more than one marker in google map (flutter)无法在谷歌地图中添加多个标记(颤振)
【发布时间】:2021-11-24 04:24:44
【问题描述】:

我正在尝试在谷歌地图中添加多个标记,但是一旦我调用该函数,只会显示最后一个标记。我错过了什么?

我读到here 说我必须传递一个动态markerId,我想我已经这样做了,但由于某种原因我无法获得第一个标记,总是最后一个。

注意:这不是错字类型的问题 - 如果我删除了 last 标记,则会显示 first 标记。所以它必须与用last一覆盖first标记的函数有关:

  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: MarkerId(_navigationList.first.userId.toString()),
      position: LatLng(_navigationList.first.latitude,
          _navigationList.first.longitude),
      icon: BitmapDescriptor.fromBytes(firstIcon),
    ));

    _listmarkers.add(Marker(
      markerId: MarkerId(_navigationList.last.userId.toString()),
      position: LatLng(_navigationList.last.latitude,
          _navigationList.last.longitude),
      icon: BitmapDescriptor.fromBytes(lastIcon),
    ));
    setState(() {});
  }

  void createMap() {
    if (_navigationList.isNotEmpty) {
      _postsController.add(1);
      LatLngBounds bound = boundsFromLatLngList(_navigationList);
      Markers();
    }
  }

【问题讨论】:

    标签: flutter google-maps dart google-maps-markers


    【解决方案1】:

    很有可能

    _navigationList.last.userId.toString() == _navigationList.first.userId.toString()
    

    【讨论】:

    • 不幸的是,事实并非如此。如果我删除 last 标记,则会显示 first 标记,并且它们在地图上具有不同的点。
    • 这如何反驳我刚刚写的内容? IMO 都具有相同的 ID。位置与它无关。
    • 我的理解是firstlast获取元素值。在我的例子中,元素的值是纬度和经度坐标,它们是不同的。因此,地图上的不同点。以下是我获取列表元素值的方法:_navigationList.addAll(value), value.forEach((element) { polylineCoordinates .add(LatLng(element.latitude, element.longitude)); }),
    • 你没有抓住重点。
    • 因为 firstlast 项目包含相同的 ID。
    猜你喜欢
    • 2020-03-14
    • 2019-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-30
    • 1970-01-01
    • 1970-01-01
    • 2021-07-13
    相关资源
    最近更新 更多