removeMarker method

void removeMarker(
  1. String markerId
)

Implementation

void removeMarker(String markerId) {
  Marker? markerToRemove;
  for (Marker marker in _markers) {
    if (marker.markerId.value == markerId) {
      markerToRemove = marker;
      break;
    }
  }
  if (markerToRemove == null) {
    return;
  }

  _markers.remove(markerToRemove);

  for (Marker marker in _customTourMainMarkers) {
    if (marker.markerId.value == markerId) {
      _customTourMainMarkers.remove(marker);
      break;
    }
  }

  List<Marker> polyLineMarkersToRemove = [];

  // _markers.removeWhere((marker) => marker.markerId.value == markerId);
  List<PolylineId> polylinesToRemove = [];
  for (Polyline polyline in _polylines) {
    LatLng start = polyline.points.first;
    LatLng end = polyline.points.last;

    if ((start.latitude == markerToRemove.position.latitude &&
            start.longitude == markerToRemove.position.longitude) ||
        (end.latitude == markerToRemove.position.latitude &&
            end.longitude == markerToRemove.position.longitude)) {
      polylinesToRemove.add(polyline.polylineId);
      if (polylineMarkers[polyline] == null) continue;

      for (Marker mrkr in polylineMarkers[polyline]!) {
        polyLineMarkersToRemove.add(mrkr);
      }
    }
  }

  for (PolylineId polylineId in polylinesToRemove) {
    _polylines.removeWhere((polyline) => polyline.polylineId == polylineId);
  }

  for (Marker mrkr in polyLineMarkersToRemove) {
    _markers.remove(mrkr);
  }
  addPolylinesBetweenMarkers();

  notifyListeners();
}