Jessica
Jessica

Reputation: 9830

Show multiple markers in google maps

I'm trying to create multiple Markers for google maps. I started out with a Set<Marker> markers = Set();, then made a loop over a List and added Markers to markers

Marker resultMarker = Marker(
            markerId: MarkerId(currentLocation.id),
            infoWindow: InfoWindow(title: "${currentLocation.title}", snippet: "$snippet"),
            position: LatLng(currentLocation.coordinates[1], currentLocation.coordinates[0]),
          );

markers.add(resultMarker);

I then tried returning a widget of:

GoogleMap(
          initialCameraPosition: CameraPosition(target: _cameraPosition, zoom: 11.0),
          onMapCreated: (controller) => mapController = controller,
          myLocationButtonEnabled: false,
          markers: markers,
);

But I only get the first marker and not all of them. How can I get all markers?

Upvotes: 0

Views: 447

Answers (1)

Payam Asefi
Payam Asefi

Reputation: 2757

This one worked for me:

List<Marker> markers = <Marker>[];

and inside your loop:

    markers.add(
      Marker(
        markerId: MarkerId(currentLocation.id),
        infoWindow: InfoWindow(title: "${currentLocation.title}", snippet: "$snippet"),
        position: LatLng(currentLocation.coordinates[1], currentLocation.coordinates[0]),

     )
   );

make sure that markers data are correct since google map does not show markers with invalid LatLng ( try to test with dummy data)

and then

GoogleMap(
      initialCameraPosition: CameraPosition(
        target: LatLng(38.9647,35.2233),
        zoom: 9.0,
      ),
      mapType: MapType.normal,
      markers: Set<Marker>.of(_markers),
      onMapCreated: (GoogleMapController controller) {
        _controller.complete(controller);
      },
    )

Upvotes: 1

Related Questions