Reputation: 718
I have a number of trips with each having a list of positions. I need assign a color for each trip but what's happening is that as the lines are being drawn, instead of changing the new color for each trip, it changes for all polylines already drawn. How can I do it for each trip?
getTrips() async {
await Future.forEach(trips, (element) async {
List positions = element['positions'] as List;
await Future.forEach(positions, (element) async {
latitude = element['coordinates'][0];
longitude = element['coordinates'][1];
direction = element['direction'] ?? 0;
//Current position
position = LatLng(latitude ?? 0, longitude ?? 0);
positionList.add(position);
addMarker();
});
});}
addMarker() async {
var m = markers.firstWhere((p) => p.markerId == MarkerId(equipmentId), orElse: () => null);
if (markers.isNotEmpty) {
markers.remove(m);
}
selectedMarker = MarkerId(equipmentId);
markers
..add(Marker(
visible: true,
markerId: selectedMarker,
position: position,
icon: customIcon,
anchor: Offset(0.5, 0.5),
rotation: direction,
));
_polyline.add(Polyline(
polylineId: PolylineId(_polylineIdCounter.toString()),
visible: true,
width: 6,
points: positionList,
color: tripColors[++colorIndex % tripColors.length],
));
_polylineIdCounter++;
cPosition = CameraPosition(
zoom: cameraZoom,
target: LatLng(position.latitude, position.longitude),
);
if (mounted) setState(() {});
_animateCamera(cPosition);
}
Upvotes: 3
Views: 1654
Reputation: 41
I know it's been a while but just in case there's someone out there who needs this then here you go.
Theoretically, the polyline property is a "Set{}" which means you can have more than one polyline, so for each trip create a different polyline with a different color and add all of them to the set.
I believe that should solve it.
Upvotes: 1