Reputation: 196
In my Flutter project, I've been working on drawing a polyline while updating a card. Generally, the implementation has been successful and the polyline appears as expected. However, I've encountered an issue where, at times, multiple polylines are displayed instead of just one. This unexpected behavior is puzzling, and I'm seeking a solution to ensure that only a single polyline is shown consistently. Any guidance or insights on resolving this issue would be greatly appreciated.
This is my code:
class MapViewProvider extends ChangeNotifier {
Set<Polyline> polyline = {};
void drawPolyline(LatLng selectedPoint) async {
//get origin lat long
LatLng? currentPoint = chooseWhereFromDrawPolyline();
if (currentPoint == null) return;
//get polyline lat long
List<LatLng>? latLong = await MyPolyline().getPolylinePoints(
currentPoint,
selectedPoint,
);
if (latLong == null) return;
//clear polyline
polyline = {};
notifyListeners();
//save to polyline
polyline = {
Polyline(
polylineId: PolylineId(polylineId),
visible: true,
points: latLong,
geodesic: false,
color: ColorsRes.dark500.withOpacity(.6),
width: 5,
startCap: Cap.roundCap,
endCap: Cap.roundCap,
),
};
notifyListeners();
}
}
GoogleMap(
initialCameraPosition: _provider.initialMapPosition(),
mapType: MapType.normal,
onMapCreated: _provider.onMapCreated,
onCameraMove: _provider.onCameraMove,
onCameraIdle: _provider.onCameraIdle,
onTap: _provider.onTap,
markers: _provider.markers,
polylines: _provider.polyline.isEmpty
? {}
: <Polyline>{_provider.polyline.last},
zoomControlsEnabled: false,
myLocationEnabled: true,
myLocationButtonEnabled: false,
)
Upvotes: 1
Views: 186