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
@@ -6,32 +6,24 @@
// Please see LICENSE files in the repository root for full details.
//
import CoreLocation
import Foundation
import MapLibre
import SwiftUI
final class LocationAnnotation: NSObject, MLNAnnotation {
final class LocationAnnotation: NSObject, MLNAnnotation, Identifiable {
let id: String
var coordinate: CLLocationCoordinate2D
let anchorPoint: CGPoint
var view: AnyView
var kind: LocationMarkerKind
// MARK: - Setup
init(id: String = UUID().uuidString,
coordinate: CLLocationCoordinate2D,
anchorPoint: CGPoint = .init(x: 0.5, y: 0.5),
@ViewBuilder label: () -> some View) {
init(id: String, coordinate: CLLocationCoordinate2D, kind: LocationMarkerKind) {
self.id = id
self.coordinate = coordinate
self.anchorPoint = anchorPoint
view = AnyView(label())
self.kind = kind
super.init()
}
func updateView(@ViewBuilder label: () -> some View) {
view = AnyView(label())
}
}
final class LocationAnnotationView: MLNUserLocationAnnotationView {
@@ -44,19 +36,21 @@ final class LocationAnnotationView: MLNUserLocationAnnotationView {
reuseIdentifier)
}
convenience init(annotation: LocationAnnotation) {
convenience init(annotation: LocationAnnotation, mediaProvider: MediaProviderProtocol?) {
self.init(annotation: annotation, reuseIdentifier: "\(Self.self)")
let hostingController = UIHostingController(rootView: annotation.view)
let markerView = LocationMarkerView(kind: annotation.kind, mediaProvider: mediaProvider)
let hostingController = UIHostingController(rootView: AnyView(markerView))
self.hostingController = hostingController
let view: UIView = hostingController.view
view.backgroundColor = .clear
view.anchorPoint = annotation.anchorPoint
view.anchorPoint = .init(x: 0.5, y: 1.0)
addSubview(view)
view.bounds.size = view.intrinsicContentSize
}
func updateContent(with view: AnyView) {
hostingController?.rootView = view
func updateContent(with kind: LocationMarkerKind, mediaProvider: MediaProviderProtocol?) {
let markerView = LocationMarkerView(kind: kind, mediaProvider: mediaProvider)
hostingController?.rootView = AnyView(markerView)
if let hostedView = hostingController?.view {
hostedView.bounds.size = hostedView.intrinsicContentSize
}
@@ -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) {