[SwiftUI] UberClone: Location Select

Junyoung ParkΒ·2022λ…„ 11μ›” 19일
0

SwiftUI

λͺ©λ‘ 보기
114/136
post-thumbnail

πŸ”΄ Let's Build UBER with SwiftUI | iOS 16 & Xcode 14

UberClone: Location Select

κ΅¬ν˜„ λͺ©ν‘œ

  • μ„ νƒν•œ μ§€μ—­μœΌλ‘œ λ·° 이동

κ΅¬ν˜„ νƒœμŠ€ν¬

  • λ·° λͺ¨λΈμ˜ ν™˜κ²½ λ³€μˆ˜ν™” β†’ 지역 검색, ν™ˆ λ·° λ“±μ—μ„œ μ „μ²΄μ μœΌλ‘œ λ™μΌν•œ μΈμŠ€ν„΄μŠ€ μ ‘κ·Ό 보μž₯
  • 지역 검색 κ²°κ³Ό μ—°κ²°
  • μ–΄λ…Έν…Œμ΄μ…˜ μ„€μ • 및 ν˜„μž¬ λ³΄μ΄λŠ” μ§€μ—­μœΌλ‘œ UI 이동

핡심 μ½”λ“œ

import SwiftUI

@main
struct UberCloneApp: App {
    @StateObject private var locationViewModel = LocationSearchViewModel()
    var body: some Scene {
        WindowGroup {
            HomeView()
                .environmentObject(locationViewModel)
        }
    }
}
  • ν™˜κ²½ λ³€μˆ˜λ‘œ λ·° λͺ¨λΈ μ ‘κ·Ό κ°€λŠ₯
func selectLocation(with location: MKLocalSearchCompletion) {
        locationSearch(with: location) { [weak self] response, error in
            guard
                error == nil,
                let item = response?.mapItems.first else { return }
            let coordinate = item.placemark.coordinate
            self?.selectedLocation = coordinate
        }
    }
  • λ·° λͺ¨λΈ λ‚΄ νŠΉμ • 지역을 κ²€μƒ‰ν•˜λŠ” ν•¨μˆ˜
 private func locationSearch(with localSearch: MKLocalSearchCompletion, completion: @escaping MKLocalSearch.CompletionHandler) {
        let searchRequest = MKLocalSearch.Request()
        searchRequest.naturalLanguageQuery = localSearch.title + localSearch.subtitle
        let search = MKLocalSearch(request: searchRequest)
        search.start(completionHandler: completion)
    }
  • 기본적으둜 μ œκ³΅ν•˜λŠ” ν•Έλ“€λŸ¬λ₯Ό 전달
func updateUIView(_ uiView: UIViewType, context: Context) {
        if let selectedLocation = viewModel.selectedLocation {
            context.coordinator.addAndSelectAnnotation(with: selectedLocation)
        }
    }
func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
            setRegion(with: userLocation.coordinate)
        }
        
        private func setRegion(with coordinate: CLLocationCoordinate2D) {
            let region = MKCoordinateRegion(center: coordinate, span: .init(latitudeDelta: 0.05, longitudeDelta: 0.05))
            parent.mapView.setRegion(region, animated: true)
        }
        
        func addAndSelectAnnotation(with coordinate: CLLocationCoordinate2D) {
            parent.mapView.removeAnnotations(parent.mapView.annotations)
            let annotation = MKPointAnnotation()
            annotation.coordinate = coordinate
            parent.mapView.addAnnotation(annotation)
            parent.mapView.selectAnnotation(annotation, animated: true)
            parent.mapView.showAnnotations(parent.mapView.annotations, animated: true)
            setRegion(with: coordinate)
        }
.onTapGesture {
                                withAnimation(.spring()) {
                                    viewModel.selectLocation(with: result)
                                    showLocationSearchView.toggle()
                                }
                            }

κ΅¬ν˜„ ν™”λ©΄

profile
JUST DO IT

0개의 λŒ“κΈ€