Skip to content

[google_maps_flutter] Changing a marker's cluster makes the marker disappear #171759

@Miiite

Description

@Miiite

Steps to reproduce

Using the google_maps_flutter example project, replace the clustering.dart file, with the provided file in code samples:

Using this version of the clustering page:

  • Add a new cluster to the map
  • Zoom into the cluster until you see a few markers
  • Click on one of the markers
  • Observe that the marker disappears from the screen
  • Zoom in & out of the map
  • The marker re-appears (green) at some point

Description of what happens:

  • When creating the map, I assign it an additional clusterManager, called 'selected':
clusterManagers: <ClusterManager>{
              const ClusterManager(
                  clusterManagerId: selectedClusterId),
              ...clusterManagers.values,
            },
  • When clicking a marker I explicitely change the marker's cluster id:
void _onMarkerTapped(MarkerId markerId) {
    final Marker? tappedMarker = markers[markerId];
    if (tappedMarker != null) {
      setState(() {
        final MarkerId? previousMarkerId = selectedMarker;
        if (previousMarkerId != null && markers.containsKey(previousMarkerId)) {
          final Marker resetOld = markers[previousMarkerId]!.copyWith(
            iconParam: BitmapDescriptor.defaultMarker,
          );
          markers[previousMarkerId] = resetOld;
        }
        selectedMarker = markerId;
        final Marker newMarker = tappedMarker.copyWith(
          iconParam: BitmapDescriptor.defaultMarkerWithHue(
            BitmapDescriptor.hueGreen,
          ),
          clusterManagerIdParam: selectedClusterId, // Assign a new clusterId
        );
        markers[markerId] = newMarker;
      });
    }
  }

This kind of manipulation can be usefull in the following scenario:

  • When a pin is selected, you want to "disable" clustering for it, to make sure it stays on screen for any zoom level available

Expected results

When clicking the marker, the marker should stay on screen and appear green.

Actual results

The marker disappears when its cluster value changes.

Code sample

Code sample

Using the google_maps_flutter example project, replace the clustering.dart file, with this version of the file:

// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:math';

import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';

import 'page.dart';

/// Page for demonstrating marker clustering support.
class ClusteringPage extends GoogleMapExampleAppPage {
  /// Default Constructor.
  const ClusteringPage({Key? key})
      : super(const Icon(Icons.place), 'Manage clustering', key: key);

  @override
  Widget build(BuildContext context) {
    return const ClusteringBody();
  }
}

/// Body of the clustering page.
class ClusteringBody extends StatefulWidget {
  /// Default Constructor.
  const ClusteringBody({super.key});

  @override
  State<StatefulWidget> createState() => ClusteringBodyState();
}

/// State of the clustering page.
class ClusteringBodyState extends State<ClusteringBody> {
  /// Default Constructor.
  ClusteringBodyState();

  /// Starting point from where markers are added.
  static const LatLng center = LatLng(-33.86, 151.1547171);

  /// Marker offset factor for randomizing marker placing.
  static const double _markerOffsetFactor = 0.05;

  /// Offset for longitude when placing markers to different cluster managers.
  static const double _clusterManagerLongitudeOffset = 0.1;

  /// Maximum amount of cluster managers.
  static const int _clusterManagerMaxCount = 3;

  /// Amount of markers to be added to the cluster manager at once.
  static const int _markersToAddToClusterManagerCount = 10;

  /// Fully visible alpha value.
  static const double _fullyVisibleAlpha = 1.0;

  /// Half visible alpha value.
  static const double _halfVisibleAlpha = 0.5;

  static const ClusterManagerId selectedClusterManagerId =
      ClusterManagerId('selected');

  /// Google map controller.
  GoogleMapController? controller;

  /// Map of clusterManagers with identifier as the key.
  Map<ClusterManagerId, ClusterManager> clusterManagers =
      <ClusterManagerId, ClusterManager>{};

  /// Map of markers with identifier as the key.
  Map<MarkerId, Marker> markers = <MarkerId, Marker>{};

  /// Id of the currently selected marker.
  MarkerId? selectedMarker;

  /// Counter for added cluster manager ids.
  int _clusterManagerIdCounter = 1;

  /// Counter for added markers ids.
  int _markerIdCounter = 1;

  /// Cluster that was tapped most recently.
  Cluster? lastCluster;

  void _onMapCreated(GoogleMapController controllerParam) {
    setState(() {
      controller = controllerParam;
    });
  }

  @override
  void dispose() {
    super.dispose();
  }

  void _onMarkerTapped(MarkerId markerId) {
    final Marker? tappedMarker = markers[markerId];
    if (tappedMarker != null) {
      setState(() {
        final MarkerId? previousMarkerId = selectedMarker;
        if (previousMarkerId != null && markers.containsKey(previousMarkerId)) {
          final Marker resetOld = markers[previousMarkerId]!.copyWith(
            iconParam: BitmapDescriptor.defaultMarker,
          );
          markers[previousMarkerId] = resetOld;
        }
        selectedMarker = markerId;
        final Marker newMarker = tappedMarker.copyWith(
          iconParam: BitmapDescriptor.defaultMarkerWithHue(
            BitmapDescriptor.hueGreen,
          ),
          clusterManagerIdParam: const ClusterManagerId('selected'),
        );
        markers[markerId] = newMarker;
      });
    }
  }

  void _addClusterManager() {
    if (clusterManagers.length == _clusterManagerMaxCount) {
      return;
    }

    final String clusterManagerIdVal =
        'cluster_manager_id_$_clusterManagerIdCounter';
    _clusterManagerIdCounter++;
    final ClusterManagerId clusterManagerId =
        ClusterManagerId(clusterManagerIdVal);

    final ClusterManager clusterManager = ClusterManager(
      clusterManagerId: clusterManagerId,
      onClusterTap: (Cluster cluster) => setState(() {
        lastCluster = cluster;
      }),
    );

    setState(() {
      clusterManagers[clusterManagerId] = clusterManager;
    });
    _addMarkersToCluster(clusterManager);
  }

  void _removeClusterManager(ClusterManager clusterManager) {
    setState(() {
      // Remove markers managed by cluster manager to be removed.
      markers.removeWhere((MarkerId key, Marker marker) =>
          marker.clusterManagerId == clusterManager.clusterManagerId);
      // Remove cluster manager.
      clusterManagers.remove(clusterManager.clusterManagerId);
    });
  }

  void _addMarkersToCluster(ClusterManager clusterManager) {
    for (int i = 0; i < _markersToAddToClusterManagerCount; i++) {
      final String markerIdVal =
          '${clusterManager.clusterManagerId.value}_marker_id_$_markerIdCounter';
      _markerIdCounter++;
      final MarkerId markerId = MarkerId(markerIdVal);

      final int clusterManagerIndex =
          clusterManagers.values.toList().indexOf(clusterManager);

      // Add additional offset to longitude for each cluster manager to space
      // out markers in different cluster managers.
      final double clusterManagerLongitudeOffset =
          clusterManagerIndex * _clusterManagerLongitudeOffset;

      final Marker marker = Marker(
        clusterManagerId: clusterManager.clusterManagerId,
        markerId: markerId,
        position: LatLng(
          center.latitude + _getRandomOffset(),
          center.longitude + _getRandomOffset() + clusterManagerLongitudeOffset,
        ),
        infoWindow: InfoWindow(title: markerIdVal, snippet: '*'),
        onTap: () => _onMarkerTapped(markerId),
      );
      markers[markerId] = marker;
    }
    setState(() {});
  }

  double _getRandomOffset() {
    return (Random().nextDouble() - 0.5) * _markerOffsetFactor;
  }

  void _remove(MarkerId markerId) {
    setState(() {
      if (markers.containsKey(markerId)) {
        markers.remove(markerId);
      }
    });
  }

  void _changeMarkersAlpha() {
    for (final MarkerId markerId in markers.keys) {
      final Marker marker = markers[markerId]!;
      final double current = marker.alpha;
      markers[markerId] = marker.copyWith(
        alphaParam: current == _fullyVisibleAlpha
            ? _halfVisibleAlpha
            : _fullyVisibleAlpha,
      );
    }
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    final MarkerId? selectedId = selectedMarker;
    return Column(
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      children: <Widget>[
        SizedBox(
          height: 300.0,
          child: GoogleMap(
            onMapCreated: _onMapCreated,
            initialCameraPosition: const CameraPosition(
              target: LatLng(-33.852, 151.25),
              zoom: 11.0,
            ),
            markers: Set<Marker>.of(markers.values),
            clusterManagers: <ClusterManager>{
              const ClusterManager(
                clusterManagerId: selectedClusterManagerId,
              ),
              ...clusterManagers.values,
            },
          ),
        ),
        Column(children: <Widget>[
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: <Widget>[
              TextButton(
                onPressed: clusterManagers.length >= _clusterManagerMaxCount
                    ? null
                    : () => _addClusterManager(),
                child: const Text('Add cluster manager'),
              ),
              TextButton(
                onPressed: clusterManagers.isEmpty
                    ? null
                    : () => _removeClusterManager(clusterManagers.values.last),
                child: const Text('Remove cluster manager'),
              ),
            ],
          ),
          Wrap(
            alignment: WrapAlignment.spaceEvenly,
            children: <Widget>[
              for (final MapEntry<ClusterManagerId, ClusterManager> clusterEntry
                  in clusterManagers.entries)
                TextButton(
                  onPressed: () => _addMarkersToCluster(clusterEntry.value),
                  child: Text('Add markers to ${clusterEntry.key.value}'),
                ),
            ],
          ),
          Wrap(
            alignment: WrapAlignment.spaceEvenly,
            children: <Widget>[
              TextButton(
                onPressed: selectedId == null
                    ? null
                    : () {
                        _remove(selectedId);
                        setState(() {
                          selectedMarker = null;
                        });
                      },
                child: const Text('Remove selected marker'),
              ),
              TextButton(
                onPressed: markers.isEmpty ? null : () => _changeMarkersAlpha(),
                child: const Text('Change all markers alpha'),
              ),
            ],
          ),
          if (lastCluster != null)
            Padding(
                padding: const EdgeInsets.all(10),
                child: Text(
                    'Cluster with ${lastCluster!.count} markers clicked at ${lastCluster!.position}')),
        ]),
      ],
    );
  }
}

Screenshots or Video

Screenshots / Video demonstration

When clicking the marker, it disappears:
Image

After a few map manipulations, it reappears in the selected state:
Image

Logs

Logs
[Paste your logs here]

Flutter Doctor output

Doctor output
[✓] Flutter (Channel stable, 3.32.5, on macOS 15.5 24F74 darwin-arm64, locale en-FR) [434ms]
    • Flutter version 3.32.5 on channel stable at /Users/adrien.padol/fvm/versions/3.32.5
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision fcf2c11572 (2 weeks ago), 2025-06-24 11:44:07 -0700
    • Engine revision dd93de6fb1
    • Dart version 3.8.1
    • DevTools version 2.45.1

[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0-rc3) [1,947ms]
    • Android SDK at /Users/adrien.padol/Library/Android/sdk
    • Platform android-35, build-tools 35.0.0-rc3
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
      This is the JDK bundled with the latest Android Studio installation on this machine.
      To manually set the JDK path, use: `flutter config --jdk-dir="path/to/jdk"`.
    • Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11572160)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 16.3) [1,179ms]
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 16E140
    • CocoaPods version 1.16.2

[✓] Chrome - develop for the web [13ms]
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2023.3) [13ms]
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11572160)

[✓] VS Code (version 1.101.2) [10ms]
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.108.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work listfound in release: 3.32Found to occur in 3.32found in release: 3.33Found to occur in 3.33has reproducible stepsThe issue has been confirmed reproducible and is ready to work onp: mapsGoogle Maps pluginpackageflutter/packages repository. See also p: labels.team-ecosystemOwned by Ecosystem teamtriaged-ecosystemTriaged by Ecosystem team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy