bpillon / google_maps_cluster_manager

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

Marker not always displayed (_mapController.getVisibleRegion does not return the right value) #11

Closed fvisticot closed 2 years ago

fvisticot commented 3 years ago

It seems the _updateMarkers functions is called on the onMapCreated / setMapController call. This call is then trigerring the getMarkers function from the clusterManager class. In this method, the _mapController.getVisibleRegion is called.

But the current Visible region does not seems updated / available at this time.

To make it works, I'm obliged to add a delay before calling the _manager.setMapController(controller);

Any idea / solution ?

onMapCreated: (GoogleMapController controller) {
            _controller.complete(controller);
            _manager.setMapController(controller);
          },
Future<List<Cluster<T>>> getMarkers() async {
    if (_mapController == null) return List();

    LatLngBounds latLngBounds = await _mapController.getVisibleRegion();
bpillon commented 3 years ago

Hello @fvisticot, I can't manager to reproduce it. You get a null value on getVisibleRegion() ? It might be a flutter_google_maps bug.

gilthonweapps commented 3 years ago

Hi,

I have the same issue using google_maps_cluster_manager 0.2.0 and google_maps_flutter 1.0.2. I have to use a delay.

onMapCreated: (GoogleMapController controller) {
                    if (_mapController.isCompleted == false) {
                      _mapController.complete(controller);
                    }
                    Future.delayed(Duration(milliseconds: 100)).then((value) => _clusterManager.setMapController(controller));
                  },
jbacco commented 1 year ago

I am still having this issue on v3.0.0+1 (Flutter: version 3.0.1). The tricky part is that it appears to be a race condition, so it doesn't always happen.

I wanted to share a solution which appears to work every time.

onMapCreated: (GoogleMapController controller) async {
  if (!_controller.isCompleted) {
    _controller.complete(controller);
    controller.getVisibleRegion().then((value) => _clusterManager.setMapId(controller.mapId));
  }
}