MarkerUpdates.from constructor

MarkerUpdates.from(
  1. Set<Marker> previous,
  2. Set<Marker> current
)

根据之前的marker列表previous和当前的marker列表current创建MakerUpdates.

Implementation

MarkerUpdates.from(Set<Marker> previous, Set<Marker> current) {
  final Map<String, Marker> previousMarkers = keyByMarkerId(previous);
  final Map<String, Marker> currentMarkers = keyByMarkerId(current);

  final Set<String> prevMarkerIds = previousMarkers.keys.toSet();
  final Set<String> currentMarkerIds = currentMarkers.keys.toSet();

  Marker idToCurrentMarker(String id) {
    return currentMarkers[id]!;
  }

  final Set<String> tempMarkerIdsToRemove =
      prevMarkerIds.difference(currentMarkerIds);

  final Set<Marker> tempMarkersToAdd = currentMarkerIds
      .difference(prevMarkerIds)
      .map(idToCurrentMarker)
      .toSet();

  bool hasChanged(Marker current) {
    final Marker? previous = previousMarkers[current.id];
    return current != previous;
  }

  final Set<Marker> tempMarkersToChange = currentMarkerIds
      .intersection(prevMarkerIds)
      .map(idToCurrentMarker)
      .where(hasChanged)
      .toSet();

  markersToAdd = tempMarkersToAdd;
  markerIdsToRemove = tempMarkerIdsToRemove;
  markersToChange = tempMarkersToChange;
}