Paul Grenyer
Paul Grenyer

Reputation: 1853

How to display map labels by default with Google Maps Flutter

I think my issue is the same as here:

Flutter show marker's infoWindowText by default in Google map widget

However, there is no answer, except for a link to another post which isn't quite what I want to do.

When my map opens in my app, it shows the markers correctly, but I also want to have all the labels displaying by default on open...

enter image description here

As the shown by one of the markers here:

enter image description here

I'm creating markers like this:

 for (Location location in _locations) {
      final locationMarker = Marker(
          markerId: MarkerId(location.id),
          position: LatLng(location.geoY, location.geoX),
          infoWindow: InfoWindow(
            title: location.name,
          ),
          onTap: () => _onTap(location),
          icon: _mapMarkers.icon(location.slug),
          draggable: false);

      markers.add(locationMarker);
    }

Then the map like this:

 Widget _map(Set<Marker> markers) => GoogleMap(
        mapType: MapType.normal,
        initialCameraPosition: CameraPosition(target: widget.mapCentre),
        markers: markers,
        ...
        onMapCreated: (controller) => _onMapCreated(controller, markers),
      );

And the in _onMapCreated, I've tried to show all the labels, but only the last in the list of markers is actually displayed:

 _onMapCreated(GoogleMapController controller, Set<Marker> markers) {
    if (mounted) {
      setState(() {
        _controller.complete(controller);
        controller.setMapStyle(_mapStyle);
        for (var marker in markers) {
          controller.showMarkerInfoWindow(marker.markerId);
        }
      });
...

I'm using:

[{
    "featureType": "poi",
    "stylers": [
    {
        "visibility": "off"
    }
    ]
}]

to remove points of interest from the maps and I am hoping I can do something similar to get marker labels to display by default, but so far I've not found anything when searching google, or that there's another solution.

Upvotes: 2

Views: 10606

Answers (1)

Guilherme
Guilherme

Reputation: 51

You can use showMarkerInfoWindow property the GoogleMapController

final GoogleMapController controller = await _controller.future;
controller.showMarkerInfoWindow(MarkerId('ID_MARKET'));

Upvotes: 5

Related Questions