Files
element-x-ios/ElementX/MapLibre/MapLibreShim/Sources/MapLibreAnnotation.swift
T
Stefan Ceriu 8753fe3980 Move the interactive map into a new MapLibreShim framework
Second step towards loading MapLibre on demand: everything that touches it now
lives in one framework, so that framework can later be left unlinked.

The interactive map view, its annotation view and the coordinate animator move
into MapLibreShim, which reaches the app through MapLibreShimProtocol. The app
side keeps its MapLibreMapView call site and becomes a thin wrapper that builds
a MapViewConfiguration, passing the app-only pieces (map style URLs, marker
views, tint colour) across as closures so the shim needs nothing from the app.

LocationAnnotation loses its MapLibre conformance and becomes a plain struct,
with the shim mapping it onto its own annotation type. MapLibre's log
forwarding moves into the shim as well, since TargetConfiguration can no longer
reach it.
2026-08-12 15:04:24 +03:00

61 lines
1.9 KiB
Swift

//
// Copyright 2026 Element Creations Ltd.
// Copyright 2023-2025 New Vector Ltd.
//
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
// Please see LICENSE files in the repository root for full details.
//
import CoreLocation
import MapLibre
import MapLibreInterface
import SwiftUI
final class MapLibreAnnotation: NSObject, MLNAnnotation, Identifiable {
let id: String
// @objc dynamic is required to make animations work
@objc dynamic var coordinate: CLLocationCoordinate2D
// MARK: - Setup
init(_ annotation: MapAnnotation) {
id = annotation.id
coordinate = annotation.coordinate
super.init()
}
}
final class MapLibreAnnotationView: MLNUserLocationAnnotationView {
private var hostingController: UIHostingController<AnyView>?
// MARK: - Setup
override init(annotation: MLNAnnotation?, reuseIdentifier: String?) {
super.init(annotation: annotation, reuseIdentifier:
reuseIdentifier)
}
convenience init(annotation: MapLibreAnnotation, content: AnyView?) {
self.init(annotation: annotation, reuseIdentifier: "\(Self.self)")
let hostingController = UIHostingController(rootView: content ?? AnyView(EmptyView()))
self.hostingController = hostingController
let view: UIView = hostingController.view
view.backgroundColor = .clear
view.anchorPoint = .init(x: 0.5, y: 1.0)
addSubview(view)
view.bounds.size = view.intrinsicContentSize
}
func updateContent(_ content: AnyView?) {
hostingController?.rootView = content ?? AnyView(EmptyView())
if let hostedView = hostingController?.view {
hostedView.bounds.size = hostedView.intrinsicContentSize
}
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError()
}
}