diff --git a/MarvelUI.xcodeproj/project.pbxproj b/MarvelUI.xcodeproj/project.pbxproj index f44a1dc..91acc85 100644 --- a/MarvelUI.xcodeproj/project.pbxproj +++ b/MarvelUI.xcodeproj/project.pbxproj @@ -20,7 +20,7 @@ C9CFFE812A151F7400708A15 /* LocationManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9CFFE802A151F7400708A15 /* LocationManager.swift */; }; C9CFFE872A153D7500708A15 /* LocationSearchActivationView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9CFFE862A153D7500708A15 /* LocationSearchActivationView.swift */; }; C9CFFE892A153FA800708A15 /* MapViewActionButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9CFFE882A153FA800708A15 /* MapViewActionButton.swift */; }; - C9CFFE8B2A15410900708A15 /* LocationSeachView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9CFFE8A2A15410900708A15 /* LocationSeachView.swift */; }; + C9CFFE8B2A15410900708A15 /* LocationSearchView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9CFFE8A2A15410900708A15 /* LocationSearchView.swift */; }; C9CFFE8D2A15447800708A15 /* LocationSearchResultCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9CFFE8C2A15447800708A15 /* LocationSearchResultCell.swift */; }; C9CFFE8F2A154F1900708A15 /* LocationSearchViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9CFFE8E2A154F1900708A15 /* LocationSearchViewModel.swift */; }; /* End PBXBuildFile section */ @@ -60,7 +60,7 @@ C9CFFE802A151F7400708A15 /* LocationManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationManager.swift; sourceTree = ""; }; C9CFFE862A153D7500708A15 /* LocationSearchActivationView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationSearchActivationView.swift; sourceTree = ""; }; C9CFFE882A153FA800708A15 /* MapViewActionButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapViewActionButton.swift; sourceTree = ""; }; - C9CFFE8A2A15410900708A15 /* LocationSeachView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationSeachView.swift; sourceTree = ""; }; + C9CFFE8A2A15410900708A15 /* LocationSearchView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationSearchView.swift; sourceTree = ""; }; C9CFFE8C2A15447800708A15 /* LocationSearchResultCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationSearchResultCell.swift; sourceTree = ""; }; C9CFFE8E2A154F1900708A15 /* LocationSearchViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationSearchViewModel.swift; sourceTree = ""; }; /* End PBXFileReference section */ @@ -230,7 +230,7 @@ isa = PBXGroup; children = ( C9CFFE862A153D7500708A15 /* LocationSearchActivationView.swift */, - C9CFFE8A2A15410900708A15 /* LocationSeachView.swift */, + C9CFFE8A2A15410900708A15 /* LocationSearchView.swift */, C9CFFE8C2A15447800708A15 /* LocationSearchResultCell.swift */, ); path = View; @@ -374,7 +374,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - C9CFFE8B2A15410900708A15 /* LocationSeachView.swift in Sources */, + C9CFFE8B2A15410900708A15 /* LocationSearchView.swift in Sources */, C9CFFE4D2A1517A200708A15 /* Persistence.swift in Sources */, C9CFFE7D2A151D2400708A15 /* HomeView.swift in Sources */, C9CFFE812A151F7400708A15 /* LocationManager.swift in Sources */, diff --git a/MarvelUI/Core/Home/View/HomeView.swift b/MarvelUI/Core/Home/View/HomeView.swift index e32feb1..93b798a 100644 --- a/MarvelUI/Core/Home/View/HomeView.swift +++ b/MarvelUI/Core/Home/View/HomeView.swift @@ -16,7 +16,7 @@ struct HomeView: View { .ignoresSafeArea() if showLocationSearchView { - LocationSeachView(showLocationSearchView: $showLocationSearchView) + LocationSearchView(showLocationSearchView: $showLocationSearchView) } else { LocationSearchActivationView() .padding(.vertical, 72) diff --git a/MarvelUI/Core/Home/View/UberMapViewRepresentable.swift b/MarvelUI/Core/Home/View/UberMapViewRepresentable.swift index df6425e..0d13b2e 100644 --- a/MarvelUI/Core/Home/View/UberMapViewRepresentable.swift +++ b/MarvelUI/Core/Home/View/UberMapViewRepresentable.swift @@ -23,8 +23,8 @@ struct UberMapViewRepresentable: UIViewRepresentable { } func updateUIView(_ uiView: UIViewType, context: Context) { - if let selectedLocation = locationViewModel.selectedLocation { - print("DEBUG: Selected location in map view \(selectedLocation)") + if let coordinate = locationViewModel.selectedLocationCoordinate { + print("DEBUG: Selected coordinate in map view \(coordinate)") } } diff --git a/MarvelUI/Core/LocationSearch/View/LocationSeachView.swift b/MarvelUI/Core/LocationSearch/View/LocationSearchView.swift similarity index 92% rename from MarvelUI/Core/LocationSearch/View/LocationSeachView.swift rename to MarvelUI/Core/LocationSearch/View/LocationSearchView.swift index 6152674..a75d290 100644 --- a/MarvelUI/Core/LocationSearch/View/LocationSeachView.swift +++ b/MarvelUI/Core/LocationSearch/View/LocationSearchView.swift @@ -7,7 +7,7 @@ import SwiftUI -struct LocationSeachView: View { +struct LocationSearchView: View { @State private var startLocationText = "" @Binding var showLocationSearchView: Bool @EnvironmentObject var viewModel: LocationSearchViewModel @@ -57,7 +57,7 @@ struct LocationSeachView: View { LocationSearchResultCell(title: result.title, subtitle: result.subtitle) .onTapGesture { showLocationSearchView.toggle() - viewModel.selectLocation(result.title) + viewModel.selectLocation(result) } } } @@ -70,6 +70,6 @@ struct LocationSeachView: View { struct LocationSeachView_Previews: PreviewProvider { static var previews: some View { - LocationSeachView(showLocationSearchView: .constant(true)) + LocationSearchView(showLocationSearchView: .constant(true)) } } diff --git a/MarvelUI/Core/LocationSearch/ViewModel/LocationSearchViewModel.swift b/MarvelUI/Core/LocationSearch/ViewModel/LocationSearchViewModel.swift index 5753ae0..993954f 100644 --- a/MarvelUI/Core/LocationSearch/ViewModel/LocationSearchViewModel.swift +++ b/MarvelUI/Core/LocationSearch/ViewModel/LocationSearchViewModel.swift @@ -11,8 +11,10 @@ import MapKit class LocationSearchViewModel: NSObject, ObservableObject { @Published var resuls = [MKLocalSearchCompletion]() - @Published var selectedLocation: String? + @Published var selectedLocationCoordinate: CLLocationCoordinate2D? + private let searchCompleter = MKLocalSearchCompleter() + var queryFragment: String = "" { didSet { searchCompleter.queryFragment = queryFragment @@ -25,9 +27,30 @@ class LocationSearchViewModel: NSObject, ObservableObject { searchCompleter.queryFragment = queryFragment } - func selectLocation(_ location: String) { - self.selectedLocation = location + + // MARK: - Helpers + func selectLocation(_ localSearch: MKLocalSearchCompletion) { + locationSearch(forLocalSearchCompletion: localSearch) { response, error in + if let error = error { + print("DEBUG: Location search error: \(error.localizedDescription)") + return + } + guard let item = response?.mapItems.first else { return } + let coordinate = item.placemark.coordinate + self.selectedLocationCoordinate = coordinate + + print("DEBUG: Location coordinates \(coordinate)") + } } + + func locationSearch(forLocalSearchCompletion localSearch: MKLocalSearchCompletion, + completion: @escaping MKLocalSearch.CompletionHandler) { + let searchRequest = MKLocalSearch.Request() + searchRequest.naturalLanguageQuery = localSearch.title.appending(localSearch.subtitle) + let search = MKLocalSearch(request: searchRequest) + search.start(completionHandler: completion) + } + } // MARK: - MKLocalSearchCompleterDelegate