feat(map): add map, add search, search currently doesn't display anything
This commit is contained in:
@@ -6,10 +6,14 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
import CoreData
|
import MapKit
|
||||||
|
|
||||||
struct ContentView: View {
|
struct ContentView: View {
|
||||||
|
|
||||||
|
@ObservedObject var viewModel = ViewModel()
|
||||||
|
|
||||||
|
@State private var position = MapCameraPosition.automatic
|
||||||
|
@State private var showSearch: Bool = true
|
||||||
// TODO: create a map
|
// TODO: create a map
|
||||||
// Add navigation to the map
|
// Add navigation to the map
|
||||||
// Display the calculated distance and how long will it take by walking
|
// 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
|
// show how long does the route take with said walking speed
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
NavigationView {
|
Map(position: $position)
|
||||||
List {
|
// .ignoresSafeArea()
|
||||||
Text("HER")
|
.sheet(isPresented: $showSearch, content: {
|
||||||
}
|
SearchView()
|
||||||
.toolbar {
|
})
|
||||||
ToolbarItem(placement: .navigationBarTrailing) {
|
// Text("This is what's set: \(viewModel.test)")
|
||||||
Text("Edit")
|
// Button(action: {
|
||||||
}
|
// save(value: "te5t3")
|
||||||
}
|
// }, label: {Text("CLICK ME")})
|
||||||
Text("Select an item")
|
}
|
||||||
}
|
|
||||||
|
func save(value: String) {
|
||||||
|
viewModel.saveValue(value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#Preview {
|
#Preview {
|
||||||
|
|||||||
67
StepMap/SearchView.swift
Normal file
67
StepMap/SearchView.swift
Normal 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
17
StepMap/ViewModel.swift
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user