Fazza
Fazza

Reputation: 21

In Flutter is there a way to toggle the visibility of the markers on google maps?

Markers properties are set to final so can not be alters. There is no setter method to change their values without trying to recreate all markers again.

I have trie the following:

  markers.update(_markerId, (Marker m) {
      print(m.markerId);
      return Marker(markerId: MarkerId("2"), infoWindow: InfoWindow(title: "Test"));
    });

The above code just hides the last marker and nothing else

Upvotes: 2

Views: 2826

Answers (1)

Pinkesh Darji
Pinkesh Darji

Reputation: 1111

You can not change the properties of the existing marker object yet because they are declared as final.

What you can do is create a copy of that marker object in another marker and then change the property and replace with it. something like this.

 final Marker marker = markers[selectedMarker];
    setState(() {
      markers[selectedMarker] = marker.copyWith(
        visibleParam: !marker.visible,
      );
    });

Credits: Code snippet taken from official google map flutter plugin example.

More examples here https://github.com/flutter/plugins/tree/master/packages/google_maps_flutter/example/lib

Upvotes: 4

Related Questions