Home > Mobile >  Cannot add more than one marker in google map (flutter)
Cannot add more than one marker in google map (flutter)

Time:10-04

I am trying to add multiple markers in google map but once I call the function, only the last marker is shown. What am I missing?

I read here that I must pass a dynamic markerId, which I think I am already doing but for some reason I cannot get the first marker, always the last one.

Note: It's not a typo type of issue - if I remove the last marker, the first marker is being displayed. So it must have something to do with the function overwriting the first marker with the last one:

  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();
    }
  }

CodePudding user response:

Most probably

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