From 1c5e8d537a68715f5fb811295bef2091fb4c4ad0 Mon Sep 17 00:00:00 2001 From: Masashi Katsumata Date: Thu, 22 Oct 2026 18:53:57 -0700 Subject: [PATCH] Pick the marker drawn under the tap, label included --- .../marker/MapboxMarkerController.swift | 66 ++++++------------- 1 file changed, 19 insertions(+), 47 deletions(-) diff --git a/Sources/MapConductorForMapbox/marker/MapboxMarkerController.swift b/Sources/MapConductorForMapbox/marker/MapboxMarkerController.swift index 4a7306a..cc486e5 100644 --- a/Sources/MapConductorForMapbox/marker/MapboxMarkerController.swift +++ b/Sources/MapConductorForMapbox/marker/MapboxMarkerController.swift @@ -3,7 +3,7 @@ import CoreGraphics import CoreLocation import Foundation import MapboxMaps -import MapConductorCore +@_spi(MapConductorDriver) import MapConductorCore import UIKit @MainActor @@ -174,33 +174,18 @@ final class MapboxMarkerController: AbstractMarkerController MarkerState? { guard let mapView else { return nil } let candidates = markerManager.allEntities().map(\.state).filter(\.draggable) - var bestState: MarkerState? - var bestDistance = CGFloat.infinity - - for state in candidates { - let coordinate = CLLocationCoordinate2D( - latitude: state.position.latitude, - longitude: state.position.longitude - ) - let markerPoint = mapView.mapboxMap.point(for: coordinate) - guard MarkerHitTest.hitsIcon( - touchScreen: screenPoint, - markerScreen: markerPoint, - state: state - ) else { continue } - let distance = hypot(screenPoint.x - markerPoint.x, screenPoint.y - markerPoint.y) - if distance < bestDistance { - bestDistance = distance - bestState = state - } - } + let mapboxMap: MapboxMap = mapView.mapboxMap + let bestState = MarkerHitTest.pick( + touchScreen: screenPoint, + candidates: candidates, + state: { $0 }, + project: { mapboxMap.point(for: CLLocationCoordinate2D(latitude: $0.latitude, longitude: $0.longitude)) } + ) return bestState }