Pravind  Raja
Pravind Raja

Reputation: 15

Argument type List<LatLng> can't be assigned to parameter type 'LatLng'

How do I define an array of markers for Flutter as the position property for the Marker widget does not accept a List of LatLng points. How I do solve this?

final List<LatLng> _markerLocations = [
    LatLng(3.082519, 101.592201),
    LatLng(3.083355, 101.589653),
    LatLng(3.08171, 101.587507),
    LatLng(3.082519, 101.592201),
  ];
void _initMarkers() async {
    Marker marker = Marker(
      markerId: MarkerId('loop_route'),
      position: _markerLocations
    );
    setState(() {
      markers.add(_markerLocations);
    });
}
void _onMapCreated(GoogleMapController controller) {
  setState((){
    mapController = controller;
    _initMarkers();
  })
}

Upvotes: 0

Views: 1628

Answers (1)

Kalpesh Kundanani
Kalpesh Kundanani

Reputation: 5763

Marker takes instance of LatLng because there can be only one LatLng for one Marker. One marker can be only at one position in map.

You will need to create as much markers as much LatLng you have.

You will have to do this:

_markerLocations.forEach((LatLng latLong){
    markers.add(Marker(
      markerId: MarkerId('loop_route'),
      position: latLong
    ));
  });

by replacing:

 markers.add(_markerLocations);

Upvotes: 1

Related Questions