feat(map): add map, add search, search currently doesn't display anything

This commit is contained in:
2024-11-23 15:26:17 +01:00
parent be56f47591
commit 0327d63d54
3 changed files with 102 additions and 12 deletions

View File

@@ -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 {

67
StepMap/SearchView.swift Normal file
View File

@@ -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()
}

17
StepMap/ViewModel.swift Normal file
View File

@@ -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
}
}