From 0327d63d549f8c8220510a5ab2884d13c4fb64eb Mon Sep 17 00:00:00 2001 From: oliverhnat Date: Sat, 23 Nov 2024 15:26:17 +0100 Subject: [PATCH] feat(map): add map, add search, search currently doesn't display anything --- StepMap/ContentView.swift | 30 +++++++++++------- StepMap/SearchView.swift | 67 +++++++++++++++++++++++++++++++++++++++ StepMap/ViewModel.swift | 17 ++++++++++ 3 files changed, 102 insertions(+), 12 deletions(-) create mode 100644 StepMap/SearchView.swift create mode 100644 StepMap/ViewModel.swift diff --git a/StepMap/ContentView.swift b/StepMap/ContentView.swift index 7e6b4dd..4bd33d1 100644 --- a/StepMap/ContentView.swift +++ b/StepMap/ContentView.swift @@ -6,10 +6,14 @@ // import SwiftUI -import CoreData +import MapKit struct ContentView: View { + @ObservedObject var viewModel = ViewModel() + + @State private var position = MapCameraPosition.automatic + @State private var showSearch: Bool = true // TODO: create a map // Add navigation to the map // Display the calculated distance and how long will it take by walking @@ -19,17 +23,19 @@ struct ContentView: View { // show how long does the route take with said walking speed var body: some View { - NavigationView { - List { - Text("HER") - } - .toolbar { - ToolbarItem(placement: .navigationBarTrailing) { - Text("Edit") - } - } - Text("Select an item") - } + Map(position: $position) +// .ignoresSafeArea() + .sheet(isPresented: $showSearch, content: { + SearchView() + }) +// Text("This is what's set: \(viewModel.test)") +// Button(action: { +// save(value: "te5t3") +// }, label: {Text("CLICK ME")}) + } + + func save(value: String) { + viewModel.saveValue(value) } } #Preview { diff --git a/StepMap/SearchView.swift b/StepMap/SearchView.swift new file mode 100644 index 0000000..371f920 --- /dev/null +++ b/StepMap/SearchView.swift @@ -0,0 +1,67 @@ +// +// SearchView.swift +// StepMap +// +// Created by Oliver Hnát on 23.11.2024. +// + +import SwiftUI +import MapKit + +struct SearchView: View { + @State private var query: String = "" + var body: some View { + VStack { + HStack { + Image(systemName: "magnifyingglass") + TextField("Search for any location", text: $query) + .autocorrectionDisabled() + .onChange(of: self.query) { + search(for: self.query) + } + } + .modifier(TextFieldGrayBackgroudColor()) + Text("\($query.wrappedValue)") + Spacer() + } + .padding() + .interactiveDismissDisabled() + + .presentationDetents([.height(200), .large]) + .presentationBackground(.regularMaterial) + .presentationBackgroundInteraction(.enabled(upThrough: .large)) + } + + func search(for text: String) { + let searchRequest = MKLocalSearch.Request() + searchRequest.naturalLanguageQuery = text + + let search = MKLocalSearch(request: searchRequest) + search.start { (response, error) in + guard let response = response else { + print("ERROR") + return + } + for item in response.mapItems { + if let name = item.name, + let location = item.placemark.location { + print("\(name): \(location.coordinate.latitude),\(location.coordinate.longitude)") + } + } + } + } +} + +struct TextFieldGrayBackgroudColor: ViewModifier { + func body(content: Content) -> some View { + content + .padding(10) + .background(.gray.opacity(0.1)) + .clipShape(RoundedRectangle(cornerRadius: 8)) + .foregroundStyle(.primary) + } +} + +#Preview { + SearchView() +} diff --git a/StepMap/ViewModel.swift b/StepMap/ViewModel.swift new file mode 100644 index 0000000..e6d52ed --- /dev/null +++ b/StepMap/ViewModel.swift @@ -0,0 +1,17 @@ +// +// ViewModel.swift +// StepMap +// +// Created by Oliver Hnát on 23.11.2024. +// + +import Foundation + +class ViewModel: ObservableObject { + @Published var test: String = UserDefaults.standard.string(forKey: "test") ?? "" + + func saveValue(_ value: String) { + UserDefaults.standard.set(value, forKey: "test") + test = value + } +}