Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ private let mapConductorTileSizePoints: Double = 256.0
private let minCosLat: Double = 0.01
private let minCosTilt: Double = 0.05

/// MapKit(iOS)のカメラの縦の視野角(度)。
///
/// MapKit は値を公開していない。iPad Pro 11 で、傾けた地図の中心と北の地点の画面位置から
/// 焦点距離を求めて実測した値(tilt 30°)。
let mapKitVerticalFieldOfView: Double = 30.0

// Convert from MapCameraPosition to MKMapCamera
public extension MapCameraPosition {
/// Pass the target map view so the camera distance is derived from the view's actual
Expand Down
6 changes: 4 additions & 2 deletions Sources/MapConductorForMapKit/MapKitMapView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,8 @@ private struct MapKitMapViewRepresentable: UIViewRepresentable {

func mapViewDidChangeVisibleRegion(_ mapView: MKMapView) {
guard isRegionChanging else { return }
let camera = mapView.toMapCameraPosition(logicalTiltHint: controller?.lastLogicalTilt)
let raw = mapView.toMapCameraPosition(logicalTiltHint: controller?.lastLogicalTilt)
let camera = controller?.reportedCamera(raw, mapView: mapView) ?? raw
state.updateCameraPosition(camera)
polylineController?.setCurrentCameraPosition(camera)
circleController?.setCurrentCameraPosition(camera)
Expand Down Expand Up @@ -767,10 +768,11 @@ private struct MapKitMapViewRepresentable: UIViewRepresentable {
farRight: geoPoint(at: CGPoint(x: mapView.bounds.maxX, y: 0), mapView: mapView)
)

return mapView.toMapCameraPosition(
let raw = mapView.toMapCameraPosition(
logicalTiltHint: controller?.lastLogicalTilt,
visibleRegion: visibleRegion
)
return controller?.reportedCamera(raw, mapView: mapView) ?? raw
}

private func geoPoint(at point: CGPoint, mapView: MKMapView) -> GeoPoint? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ final class MapKitViewController: MapViewControllerProtocol {
let overlayControllers = OverlayControllerRegistry()
private weak var mapView: MKMapView?
private(set) var lastLogicalTilt: Double?
/// 直近の移動で要求されたパディング。MapKit のカメラはパディングを持たないので、中心をずらして
/// 再現し(``CameraPaddingEmulation``)、読み戻しで逆にずらすときに使う。
private(set) var lastPaddings: MapPaddingsProtocol?
/// アニメーションの終わりに、ターゲットの位置を投影で詰め直すための要求。
private var cameraAnimationRequested: MapCameraPosition?
private let tileSizePoints: Double = 256.0
private let minCosLat: Double = 0.01

Expand Down Expand Up @@ -96,9 +101,11 @@ final class MapKitViewController: MapViewControllerProtocol {
mapInitializedListener = listener
}

func moveCamera(position: MapCameraPosition) {
func moveCamera(position requested: MapCameraPosition) {
cancelCameraAnimation()
guard let mapView = mapView else { return }
lastPaddings = requested.paddings
let position = paddingApplied(requested, mapView: mapView)
lastLogicalTilt = position.tilt
if abs(position.tilt) < 0.01, mapView.bounds.isEmpty {
DispatchQueue.main.async { [weak self, weak mapView] in
Expand All @@ -107,20 +114,26 @@ final class MapKitViewController: MapViewControllerProtocol {
}
}
if applyTopDownZoom(position: position, mapView: mapView, animated: false) {
correctPaddedTarget(requested, mapView: mapView)
return
}
let camera = position.toMKMapCamera(on: mapView)
mapView.setCamera(camera, animated: false)
correctPaddedTarget(requested, mapView: mapView)
}

func animateCamera(position: MapCameraPosition, duration: Long) {
func animateCamera(position requested: MapCameraPosition, duration: Long) {
cancelCameraAnimation()
guard let mapView = mapView else { return }
let durationSeconds = Double(duration) / 1000.0
guard durationSeconds > 0 else {
moveCamera(position: position)
moveCamera(position: requested)
return
}
lastPaddings = requested.paddings
// 補間は「画面中心」の空間で行う(from も中心のまま読む)。
let position = paddingApplied(requested, mapView: mapView)
cameraAnimationRequested = requested

let from = mapView.toMapCameraPosition(logicalTiltHint: lastLogicalTilt)
lastLogicalTilt = position.tilt
Expand Down Expand Up @@ -265,6 +278,7 @@ final class MapKitViewController: MapViewControllerProtocol {
}

private func cancelCameraAnimation() {
cameraAnimationRequested = nil
cameraAnimationDisplayLink?.invalidate()
cameraAnimationDisplayLink = nil
cameraAnimationFrames.removeAll()
Expand Down Expand Up @@ -295,12 +309,63 @@ final class MapKitViewController: MapViewControllerProtocol {
let frame = cameraAnimationFrames[targetIndex]
applyCameraFrame(frame, mapView: mapView)
}
if progress >= 1.0, let requested = cameraAnimationRequested {
cameraAnimationRequested = nil
correctPaddedTarget(requested, mapView: mapView)
}

if progress >= 1.0 {
cancelCameraAnimation()
}
}

/// MapKit へ渡すカメラ。パディングの分だけ中心をずらす。
private func paddingApplied(_ position: MapCameraPosition, mapView: MKMapView) -> MapCameraPosition {
CameraPaddingEmulation.applied(to: position, fov: mapKitVerticalFieldOfView, viewportSize: mapView.bounds.size)
}

/// MapKit から読み戻したカメラ(画面中心)を、ターゲット・パディング・視野角を持つカメラへ戻す。
/// ターゲットはパディングの残りの中央にある地点を地図に直接聞く。
func reportedCamera(_ camera: MapCameraPosition, mapView: MKMapView) -> MapCameraPosition {
CameraPaddingEmulation.restored(
camera,
paddings: lastPaddings,
fov: mapKitVerticalFieldOfView,
viewportSize: mapView.bounds.size,
unproject: { [weak mapView] point in
guard let mapView else { return nil }
let coordinate = mapView.convert(point, toCoordinateFrom: mapView)
return GeoPoint(latitude: coordinate.latitude, longitude: coordinate.longitude)
}
)
}

/// 計算で置いた中心だと、MapKit の縮尺のずれや pitch の上限(高度で変わる)でターゲットが
/// ずれる。地図の投影で実際の位置を測り、パディングの残りの中央に来るまで中心だけを動かし直す。
private func correctPaddedTarget(_ requested: MapCameraPosition, mapView: MKMapView, remaining: Int = 3) {
guard remaining > 0 else { return }
let target = CLLocationCoordinate2D(latitude: requested.position.latitude, longitude: requested.position.longitude)
guard let center = CameraPaddingEmulation.correctedCenter(
target: requested.position,
paddings: requested.paddings,
viewportSize: mapView.bounds.size,
project: { _ in mapView.convert(target, toPointTo: mapView) },
unproject: { point in
let coordinate = mapView.convert(point, toCoordinateFrom: mapView)
return GeoPoint(latitude: coordinate.latitude, longitude: coordinate.longitude)
}
) else { return }
// 1 回ずつ次のランループで当てる。同じランループで続けて setCamera すると MapKit は
// 領域変更の通知をまとめてしまい、state に途中のカメラが残る。
DispatchQueue.main.async { [weak self, weak mapView] in
guard let self, let mapView else { return }
let camera = mapView.camera.copy() as! MKMapCamera
camera.centerCoordinate = CLLocationCoordinate2D(latitude: center.latitude, longitude: center.longitude)
mapView.setCamera(camera, animated: false)
self.correctPaddedTarget(requested, mapView: mapView, remaining: remaining - 1)
}
}

private func applyCameraFrame(_ position: MapCameraPosition, mapView: MKMapView) {
lastLogicalTilt = position.tilt
// One-step move (no MapKit internal animation). This is the basis for our custom duration-controlled animation.
Expand Down
Loading