Skip to content

Commit

Permalink
getting coordinates
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasJorgeHubert committed May 17, 2023
1 parent ffaa2bd commit ebb3a19
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 13 deletions.
8 changes: 4 additions & 4 deletions MarvelUI.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -60,7 +60,7 @@
C9CFFE802A151F7400708A15 /* LocationManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationManager.swift; sourceTree = "<group>"; };
C9CFFE862A153D7500708A15 /* LocationSearchActivationView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationSearchActivationView.swift; sourceTree = "<group>"; };
C9CFFE882A153FA800708A15 /* MapViewActionButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapViewActionButton.swift; sourceTree = "<group>"; };
C9CFFE8A2A15410900708A15 /* LocationSeachView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationSeachView.swift; sourceTree = "<group>"; };
C9CFFE8A2A15410900708A15 /* LocationSearchView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationSearchView.swift; sourceTree = "<group>"; };
C9CFFE8C2A15447800708A15 /* LocationSearchResultCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationSearchResultCell.swift; sourceTree = "<group>"; };
C9CFFE8E2A154F1900708A15 /* LocationSearchViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationSearchViewModel.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -230,7 +230,7 @@
isa = PBXGroup;
children = (
C9CFFE862A153D7500708A15 /* LocationSearchActivationView.swift */,
C9CFFE8A2A15410900708A15 /* LocationSeachView.swift */,
C9CFFE8A2A15410900708A15 /* LocationSearchView.swift */,
C9CFFE8C2A15447800708A15 /* LocationSearchResultCell.swift */,
);
path = View;
Expand Down Expand Up @@ -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 */,
Expand Down
2 changes: 1 addition & 1 deletion MarvelUI/Core/Home/View/HomeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct HomeView: View {
.ignoresSafeArea()

if showLocationSearchView {
LocationSeachView(showLocationSearchView: $showLocationSearchView)
LocationSearchView(showLocationSearchView: $showLocationSearchView)
} else {
LocationSearchActivationView()
.padding(.vertical, 72)
Expand Down
4 changes: 2 additions & 2 deletions MarvelUI/Core/Home/View/UberMapViewRepresentable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import SwiftUI

struct LocationSeachView: View {
struct LocationSearchView: View {
@State private var startLocationText = ""
@Binding var showLocationSearchView: Bool
@EnvironmentObject var viewModel: LocationSearchViewModel
Expand Down Expand Up @@ -57,7 +57,7 @@ struct LocationSeachView: View {
LocationSearchResultCell(title: result.title, subtitle: result.subtitle)
.onTapGesture {
showLocationSearchView.toggle()
viewModel.selectLocation(result.title)
viewModel.selectLocation(result)
}
}
}
Expand All @@ -70,6 +70,6 @@ struct LocationSeachView: View {

struct LocationSeachView_Previews: PreviewProvider {
static var previews: some View {
LocationSeachView(showLocationSearchView: .constant(true))
LocationSearchView(showLocationSearchView: .constant(true))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit ebb3a19

Please sign in to comment.