bpillon / google_maps_cluster_manager

Simple Flutter clustering library for Google Maps
https://pub.dev/packages/google_maps_cluster_manager
MIT License
121 stars 94 forks source link

onCameraIdle: _manager.updateMap remove all the clusters #43

Closed ChurikiTenna closed 2 years ago

ChurikiTenna commented 2 years ago

Hi, thank you for the great library! I was following the readme, and it seems my code is not working properly.

At first, clusters are shown with no problem, but once I move the map, all of them disapprear. I could not find any relative issue on anywhere..

This is my code.

class SaunaMapState extends State<SaunaMap> {
  final Completer<GoogleMapController> _controller = Completer();
  BitmapDescriptor? pinLocationIcon;

  late ClusterManager _manager;

  CameraPosition kGooglePlex = const CameraPosition(
    target: LatLng(37.5, 134.5),
    zoom: 5,
  );
  Set<Marker> markers = Set();

  @override
  void initState() {
    super.initState();
    _manager = _initClusterManager();
  }

  ClusterManager _initClusterManager() {
    print('_initClusterManager ${saunaLocs.length}');
    return ClusterManager<Place>(
      [<locations>],
      _updateMarkers,
      markerBuilder: _markerBuilder,
      stopClusteringZoom: 17.0
    );
  }

  void _updateMarkers(Set<Marker> markers) {
    print('Updated ${markers.length} markers');
    setState(() {
      this.markers = markers;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: GoogleMap(
            mapType: MapType.normal,
            markers: markers, //_createMarker(),
            initialCameraPosition: kGooglePlex,
            onMapCreated: (GoogleMapController controller) {
              _controller.complete(controller);
              _manager.setMapId(controller.mapId);
            },
            onCameraMove: _manager.onCameraMove,
            onCameraIdle: _manager.updateMap
            ),
            floatingActionButtonLocation: FloatingActionButtonLocation.startDocked,
        floatingActionButton: Button.image(
            const Icon(
              Icons.list,
              color: Colors.white,
            ), () {
          Navigator.pop(context);
        }));
  }

  Future<Marker> Function(Cluster<Place>) get _markerBuilder =>
      (cluster) async {
        return Marker(
          markerId: MarkerId(cluster.getId()),
          position: cluster.location,
          onTap: () {
            print('---- ${cluster.items}');
            cluster.items.forEach((p) => print(p));
          },
          icon: await _getMarkerBitmap(cluster.isMultiple ? 125 : 75,
              text: cluster.isMultiple ? cluster.count.toString() : null),
        );
      };

  Future<BitmapDescriptor> _getMarkerBitmap(int size, {String? text}) async {
   ..copied from readme
  }
}
ChurikiTenna commented 2 years ago

I was doing _manager = _initClusterManager() after setItems, then it reflected empty items after updateMap. Damn mistake.