Skip to content

Commit bc30741

Browse files
author
Roy
committed
feat: add auto camera fit trigger for map routes #16
1 parent de68bbd commit bc30741

3 files changed

Lines changed: 68 additions & 2 deletions

File tree

‎Projects/Presentation/Home/Sources/Components/NaverMapComponent.swift‎

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public struct NaverMapComponent: UIViewRepresentable {
2424
let spots: [ExploreMapSpot]
2525
let selectedSpotID: String?
2626
let returnToLocationTrigger: Int
27+
let autoFitTrigger: Int // 카메라 자동 조정 트리거
2728
let onSpotTapped: ((String) -> Void)?
2829
let onMapTapped: (() -> Void)?
2930
let onCameraIdle: ((CLLocationCoordinate2D) -> Void)?
@@ -38,6 +39,7 @@ public struct NaverMapComponent: UIViewRepresentable {
3839
private static var lastDestinationKey: String?
3940
private static var lastReturnToLocationTrigger: Int?
4041
private static var lastAutoFitKey: String?
42+
private static var lastAutoFitTrigger: Int?
4143
private static var routePath: NMFPath?
4244

4345
public init(
@@ -49,6 +51,7 @@ public struct NaverMapComponent: UIViewRepresentable {
4951
spots: [ExploreMapSpot] = [],
5052
selectedSpotID: String? = nil,
5153
returnToLocationTrigger: Int = 0,
54+
autoFitTrigger: Int = 0,
5255
onSpotTapped: ((String) -> Void)? = nil,
5356
onMapTapped: (() -> Void)? = nil,
5457
onCameraIdle: ((CLLocationCoordinate2D) -> Void)? = nil
@@ -61,6 +64,7 @@ public struct NaverMapComponent: UIViewRepresentable {
6164
self.spots = spots
6265
self.selectedSpotID = selectedSpotID
6366
self.returnToLocationTrigger = returnToLocationTrigger
67+
self.autoFitTrigger = autoFitTrigger
6468
self.onSpotTapped = onSpotTapped
6569
self.onMapTapped = onMapTapped
6670
self.onCameraIdle = onCameraIdle
@@ -160,6 +164,7 @@ public struct NaverMapComponent: UIViewRepresentable {
160164
let shouldReturnToLocation =
161165
currentLocation != nil
162166
&& Self.lastReturnToLocationTrigger != returnToLocationTrigger
167+
let shouldAutoFit = Self.lastAutoFitTrigger != autoFitTrigger && autoFitTrigger > 0
163168
let shouldPrioritizeCurrentLocation = shouldReturnToLocation
164169
let autoFitKey = makeAutoFitKey(destination: destination, spots: spots)
165170
Self.routePath?.mapView = nil
@@ -422,6 +427,30 @@ public struct NaverMapComponent: UIViewRepresentable {
422427
adjustCameraToFitRoute(mapView: uiView, routeCoords: pathCoords, currentLocation: currentLocation)
423428

424429
}
430+
431+
// autoFitTrigger가 변경되었을 때 카메라 자동 조정
432+
if shouldAutoFit {
433+
Self.lastAutoFitTrigger = autoFitTrigger
434+
435+
// 경로가 있으면 경로에 맞게 카메라 조정
436+
if let routeInfo = routeInfo, !routeInfo.paths.isEmpty {
437+
let pathCoords = routeInfo.paths.map {
438+
NMGLatLng(lat: $0.latitude, lng: $0.longitude)
439+
}
440+
adjustCameraToFitRoute(mapView: uiView, routeCoords: pathCoords, currentLocation: currentLocation)
441+
}
442+
// 경로가 없고 목적지가 있으면 목적지와 현재 위치에 맞게 조정
443+
else if let destination = destination {
444+
var coords = [NMGLatLng(lat: destination.coordinate.latitude, lng: destination.coordinate.longitude)]
445+
if let currentLocation = currentLocation {
446+
coords.append(NMGLatLng(lat: currentLocation.coordinate.latitude, lng: currentLocation.coordinate.longitude))
447+
}
448+
if let travelStation = travelStation {
449+
coords.append(NMGLatLng(lat: travelStation.coordinate.latitude, lng: travelStation.coordinate.longitude))
450+
}
451+
adjustCameraToFitCoordinates(mapView: uiView, coordinates: coords)
452+
}
453+
}
425454
}
426455

427456
public final class Coordinator: NSObject, NMFMapViewTouchDelegate, NMFMapViewCameraDelegate {
@@ -676,6 +705,37 @@ public struct NaverMapComponent: UIViewRepresentable {
676705
}
677706
}
678707

708+
// 여러 좌표에 맞게 카메라 조정
709+
private func adjustCameraToFitCoordinates(mapView: NMFMapView, coordinates: [NMGLatLng]) {
710+
guard let first = coordinates.first else { return }
711+
712+
var minLat = first.lat
713+
var maxLat = first.lat
714+
var minLng = first.lng
715+
var maxLng = first.lng
716+
717+
for coord in coordinates {
718+
minLat = min(minLat, coord.lat)
719+
maxLat = max(maxLat, coord.lat)
720+
minLng = min(minLng, coord.lng)
721+
maxLng = max(maxLng, coord.lng)
722+
}
723+
724+
// 약간의 여백 추가 (15%)
725+
let latPadding = (maxLat - minLat) * 0.15
726+
let lngPadding = (maxLng - minLng) * 0.15
727+
728+
let bounds = NMGLatLngBounds(
729+
southWest: NMGLatLng(lat: minLat - latPadding, lng: minLng - lngPadding),
730+
northEast: NMGLatLng(lat: maxLat + latPadding, lng: maxLng + lngPadding)
731+
)
732+
733+
// 카메라를 bounds에 맞게 조정
734+
let cameraUpdate = NMFCameraUpdate(fit: bounds, paddingInsets: UIEdgeInsets(top: 100, left: 50, bottom: 100, right: 50))
735+
cameraUpdate.animationDuration = 1.0
736+
mapView.moveCamera(cameraUpdate)
737+
}
738+
679739
// 경로 전체가 보이도록 카메라 조정
680740
private func adjustCameraToFitRoute(mapView: NMFMapView, routeCoords: [NMGLatLng], currentLocation: CLLocation?) {
681741
var allCoords = routeCoords
@@ -797,6 +857,7 @@ public struct NaverMapComponent: UIViewRepresentable {
797857
travelStation: nil as Destination?,
798858
spots: [],
799859
selectedSpotID: nil as String?,
800-
returnToLocationTrigger: 0
860+
returnToLocationTrigger: 0,
861+
autoFitTrigger: 0
801862
)
802863
}

‎Projects/Presentation/Home/Sources/Explore/View/ExploreView.swift‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ private extension ExploreView {
6969
? nil
7070
: store.userSession.selectedExploreSpotID,
7171
returnToLocationTrigger: store.returnToCurrentLocationTrigger,
72+
autoFitTrigger: 0, // ExploreView에서는 자동 피팅 사용하지 않음
7273
onSpotTapped: { spotID in
7374
store.send(.view(.spotTapped(spotID)))
7475
},

‎Projects/Presentation/Home/Sources/Route/View/RouteView.swift‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ private extension RouteView {
6868
let travelStation = store.isLoadingRoute ? nil : makeTravelStation()
6969
let routeInfo = store.isLoadingRoute ? nil : store.routeInfo
7070

71+
// 경로 정보가 업데이트되면 카메라 자동 조정 트리거 증가
72+
let autoFitTrigger = store.routeInfo != nil ? 1 : 0
73+
7174
NaverMapComponent(
7275
locationPermissionStatus: store.locationPermissionStatus,
7376
currentLocation: store.currentLocation,
@@ -76,7 +79,8 @@ private extension RouteView {
7679
travelStation: travelStation,
7780
spots: [],
7881
selectedSpotID: nil,
79-
returnToLocationTrigger: 0
82+
returnToLocationTrigger: 0,
83+
autoFitTrigger: autoFitTrigger
8084
)
8185
}
8286

0 commit comments

Comments
 (0)