From f93e02e3cc67efcd1220f3d60ac08ee227d3318c Mon Sep 17 00:00:00 2001 From: Masashi Katsumata Date: Wed, 11 Nov 2026 11:09:18 -0800 Subject: [PATCH 1/2] Apply marker zIndex through zPriority Annotation views had no zPriority, so MapKit chose the order and zIndex did nothing. Map MarkerDrawOrder onto zPriority (500 + 5 x key, zIndex clamped to +-99) and reset it on reused views. --- .../MapConductorForMapKit/MapKitMapView.swift | 2 ++ .../marker/MapKitMarkerRenderer.swift | 20 ++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/Sources/MapConductorForMapKit/MapKitMapView.swift b/Sources/MapConductorForMapKit/MapKitMapView.swift index 5f659cf..d714628 100644 --- a/Sources/MapConductorForMapKit/MapKitMapView.swift +++ b/Sources/MapConductorForMapKit/MapKitMapView.swift @@ -583,6 +583,8 @@ private struct MapKitMapViewRepresentable: UIViewRepresentable { y: -(resolvedIcon.anchor.y - 0.5) * resolvedIcon.bitmap.size.height ) annotationView.alpha = 1 + // 使い回したビューに前のマーカーの順位を残さない。 + annotationView.zPriority = .defaultUnselected } return annotationView diff --git a/Sources/MapConductorForMapKit/marker/MapKitMarkerRenderer.swift b/Sources/MapConductorForMapKit/marker/MapKitMarkerRenderer.swift index b325ab9..725321b 100644 --- a/Sources/MapConductorForMapKit/marker/MapKitMarkerRenderer.swift +++ b/Sources/MapConductorForMapKit/marker/MapKitMarkerRenderer.swift @@ -1,7 +1,7 @@ import CoreGraphics import CoreLocation import MapKit -import MapConductorCore +@_spi(MapConductorDriver) import MapConductorCore @MainActor final class MapKitMarkerRenderer: MarkerOverlayRendererProtocol { @@ -283,6 +283,21 @@ final class MapKitMarkerRenderer: MarkerOverlayRendererProtocol { mapView = nil } + /// ``MarkerDrawOrder`` を MapKit の `zPriority`(Float、0...1000、既定 500、大きいほど手前)へ写す。 + /// + /// `500 + 5 × key`。zIndex は ±``zPriorityZIndexSpan`` に丸める(それを超える zIndex どうしは + /// 区別しない)。1 段を 5 にするのは、同じ zIndex の中の南・西の順位(key の小数部)を + /// Float でも数百 m 程度まで区別できるようにするため。選択中のビューは MapKit が + /// `selectedZPriority`(1000)へ上げる。 + static func zPriority(for state: MarkerState) -> MKAnnotationViewZPriority { + let zIndex = state.zIndex.map { min(max($0, -zPriorityZIndexSpan), zPriorityZIndexSpan) } + let key = MarkerDrawOrder.key(zIndex: zIndex, position: state.position) + return MKAnnotationViewZPriority(rawValue: Float(500.0 + 5.0 * key)) + } + + /// ``zPriority(for:)`` が区別する zIndex の絶対値の上限。 + static let zPriorityZIndexSpan = 99 + func configureAnnotationView(_ view: MKAnnotationView, for state: MarkerState, bitmapIcon: BitmapIcon) { view.image = bitmapIcon.bitmap view.isDraggable = state.draggable @@ -294,6 +309,9 @@ final class MapKitMarkerRenderer: MarkerOverlayRendererProtocol { // Store for later reference markerAnnotationViews[state.id] = view + // 重なり順。ビューは使い回されるので、毎回書く(書かないと前のマーカーの値が残る)。 + view.zPriority = Self.zPriority(for: state) + // Set anchor point based on icon anchor // MapKit's centerOffset is from the annotation coordinate to the view center // So y-axis needs to be inverted From 5ec3e05c580a66b7d64b0a685863a93224769901 Mon Sep 17 00:00:00 2001 From: Masashi Katsumata Date: Wed, 11 Nov 2026 14:55:56 -0800 Subject: [PATCH 2/2] Re-key annotation zPriority when the map rotates Same-zIndex markers stack lower-on-screen in front. On a heading change the renderer rewrites zPriority on the visible annotation views; views created later pick up the current frame. --- .../marker/MapKitMarkerRenderer.swift | 27 +++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/Sources/MapConductorForMapKit/marker/MapKitMarkerRenderer.swift b/Sources/MapConductorForMapKit/marker/MapKitMarkerRenderer.swift index 725321b..a1ad08f 100644 --- a/Sources/MapConductorForMapKit/marker/MapKitMarkerRenderer.swift +++ b/Sources/MapConductorForMapKit/marker/MapKitMarkerRenderer.swift @@ -4,13 +4,15 @@ import MapKit @_spi(MapConductorDriver) import MapConductorCore @MainActor -final class MapKitMarkerRenderer: MarkerOverlayRendererProtocol { +final class MapKitMarkerRenderer: MarkerOverlayRendererProtocol, MarkerDrawOrderFrameReceiver { typealias ActualMarker = MKPointAnnotation private static let maxConcurrentAnimations = 30 private static let minAnimationUpdateInterval: CFTimeInterval = 1.0 / 4.0 weak var mapView: MKMapView? + /// 重なり順を出すときの地図の向き(``MarkerDrawOrder``)。回すとコントローラが差し替える。 + private var drawOrderFrame: MarkerDrawOrder.Frame = .northUp private let markerManager: MarkerManager private var markerAnnotationViews: [String: MKAnnotationView] = [:] private var markerAnimationRunners: [String: MarkerAnimationRunner] = [:] @@ -286,15 +288,30 @@ final class MapKitMarkerRenderer: MarkerOverlayRendererProtocol { /// ``MarkerDrawOrder`` を MapKit の `zPriority`(Float、0...1000、既定 500、大きいほど手前)へ写す。 /// /// `500 + 5 × key`。zIndex は ±``zPriorityZIndexSpan`` に丸める(それを超える zIndex どうしは - /// 区別しない)。1 段を 5 にするのは、同じ zIndex の中の南・西の順位(key の小数部)を + /// 区別しない)。1 段を 5 にするのは、同じ zIndex の中の画面上の順位(key の小数部)を /// Float でも数百 m 程度まで区別できるようにするため。選択中のビューは MapKit が /// `selectedZPriority`(1000)へ上げる。 - static func zPriority(for state: MarkerState) -> MKAnnotationViewZPriority { + static func zPriority( + for state: MarkerState, + frame: MarkerDrawOrder.Frame = .northUp + ) -> MKAnnotationViewZPriority { let zIndex = state.zIndex.map { min(max($0, -zPriorityZIndexSpan), zPriorityZIndexSpan) } - let key = MarkerDrawOrder.key(zIndex: zIndex, position: state.position) + let key = MarkerDrawOrder.key(zIndex: zIndex, position: state.position, frame: frame) return MKAnnotationViewZPriority(rawValue: Float(500.0 + 5.0 * key)) } + /// 回したら、同じ zIndex の中の順位(画面で下ほど手前)を付け直す。画面外でビューの無い + /// マーカーは、ビューが付くときに ``configureAnnotationView(_:for:bitmapIcon:)`` が新しい向きで書く。 + func onDrawOrderFrameChanged(_ frame: MarkerDrawOrder.Frame, entities: [MarkerEntity]) async { + drawOrderFrame = frame + guard let mapView else { return } + for entity in entities { + guard let annotation = entity.marker, let view = mapView.view(for: annotation) else { continue } + let priority = Self.zPriority(for: entity.state, frame: frame) + if view.zPriority != priority { view.zPriority = priority } + } + } + /// ``zPriority(for:)`` が区別する zIndex の絶対値の上限。 static let zPriorityZIndexSpan = 99 @@ -310,7 +327,7 @@ final class MapKitMarkerRenderer: MarkerOverlayRendererProtocol { markerAnnotationViews[state.id] = view // 重なり順。ビューは使い回されるので、毎回書く(書かないと前のマーカーの値が残る)。 - view.zPriority = Self.zPriority(for: state) + view.zPriority = Self.zPriority(for: state, frame: drawOrderFrame) // Set anchor point based on icon anchor // MapKit's centerOffset is from the annotation coordinate to the view center