Render LiveLocations in the map (#5394)

This commit is contained in:
Mauro
2026-04-14 18:27:34 +02:00
committed by GitHub
parent a40fab5f5e
commit 602d779e3a
19 changed files with 380 additions and 163 deletions
@@ -20,10 +20,10 @@ struct MapLibreMapView: UIViewRepresentable {
/// The initial map center
let mapCenter: CLLocationCoordinate2D
/// Map annotations keyed by a stable identifier (e.g. sender userID for user pins, UUID string for generic pins)
let annotations: [String: LocationAnnotation]
/// Map annotations
let annotations: [LocationAnnotation]
init(zoomLevel: Double, initialZoomLevel: Double, mapCenter: CLLocationCoordinate2D, annotations: [String: LocationAnnotation] = [:]) {
init(zoomLevel: Double, initialZoomLevel: Double, mapCenter: CLLocationCoordinate2D, annotations: [LocationAnnotation] = []) {
self.zoomLevel = zoomLevel
self.initialZoomLevel = initialZoomLevel
self.mapCenter = mapCenter
@@ -39,6 +39,8 @@ struct MapLibreMapView: UIViewRepresentable {
let options: Options
let mediaProvider: MediaProviderProtocol?
/// Behavior mode of the current user's location, can be hidden, only shown and shown following the user
@Binding var showsUserLocationMode: ShowUserLocationMode
/// Bind view errors if any
@@ -86,7 +88,7 @@ struct MapLibreMapView: UIViewRepresentable {
// MARK: - Private
private func setupMap(mapView: MLNMapView, with options: Options) {
mapView.addAnnotations(Array(options.annotations.values))
mapView.addAnnotations(options.annotations)
mapView.zoomLevel = options.annotations.isEmpty ? options.initialZoomLevel : options.zoomLevel
mapView.centerCoordinate = options.mapCenter
}
@@ -94,9 +96,10 @@ struct MapLibreMapView: UIViewRepresentable {
private func updateAnnotations(in mapView: MLNMapView) {
let existingByID = Dictionary(uniqueKeysWithValues:
(mapView.annotations ?? []).compactMap { $0 as? LocationAnnotation }.map { ($0.id, $0) })
let updatedByID = Dictionary(uniqueKeysWithValues: options.annotations.map { ($0.id, $0) })
let existingIDs = Set(existingByID.keys)
let updatedIDs = Set(options.annotations.keys)
let updatedIDs = Set(updatedByID.keys)
// Remove annotations that are no longer present
let removedIDs = existingIDs.subtracting(updatedIDs)
@@ -108,7 +111,7 @@ struct MapLibreMapView: UIViewRepresentable {
// Add new annotations
let addedIDs = updatedIDs.subtracting(existingIDs)
if !addedIDs.isEmpty {
let toAdd = addedIDs.compactMap { options.annotations[$0] }
let toAdd = addedIDs.compactMap { updatedByID[$0] }
mapView.addAnnotations(toAdd)
}
@@ -116,12 +119,12 @@ struct MapLibreMapView: UIViewRepresentable {
let keptIDs = existingIDs.intersection(updatedIDs)
for id in keptIDs {
guard let existingAnnotation = existingByID[id],
let updatedAnnotation = options.annotations[id] else {
let updatedAnnotation = updatedByID[id] else {
continue
}
existingAnnotation.coordinate = updatedAnnotation.coordinate
if let annotationView = mapView.view(for: existingAnnotation) as? LocationAnnotationView {
annotationView.updateContent(with: updatedAnnotation.view)
annotationView.updateContent(with: updatedAnnotation.kind, mediaProvider: mediaProvider)
}
}
}
@@ -179,7 +182,7 @@ extension MapLibreMapView {
guard let annotation = annotation as? LocationAnnotation else {
return nil
}
return LocationAnnotationView(annotation: annotation)
return LocationAnnotationView(annotation: annotation, mediaProvider: mapLibreView.mediaProvider)
}
func mapViewDidFailLoadingMap(_ mapView: MLNMapView, withError error: Error) {