feat(search): search for a location and get directions to it
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
# StepMap
|
||||
## TODO:
|
||||
- [ ] Create a map
|
||||
- [x] Create a map
|
||||
- [ ] Add navigation to the map
|
||||
- [ ] Display the calculated distance and how long will it take by walking
|
||||
- [ ] Get walkingStepLength from HealthKit
|
||||
|
||||
17
StepMap/SearchItemView.swift
Normal file
17
StepMap/SearchItemView.swift
Normal file
@@ -0,0 +1,17 @@
|
||||
//
|
||||
// SearchItemView.swift
|
||||
// StepMap
|
||||
//
|
||||
// Created by Oliver Hnát on 23.11.2024.
|
||||
//
|
||||
|
||||
import MapKit
|
||||
import SwiftUI
|
||||
|
||||
struct SearchItemView: View {
|
||||
var location: MKMapItem
|
||||
|
||||
var body: some View {
|
||||
Text("\(location.name ?? "")")
|
||||
}
|
||||
}
|
||||
@@ -5,11 +5,13 @@
|
||||
// Created by Oliver Hnát on 23.11.2024.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
import MapKit
|
||||
import SwiftUI
|
||||
|
||||
struct SearchView: View {
|
||||
@State private var query: String = ""
|
||||
@State private var locations: [MKMapItem] = []
|
||||
|
||||
var body: some View {
|
||||
VStack {
|
||||
HStack {
|
||||
@@ -21,33 +23,70 @@ struct SearchView: View {
|
||||
}
|
||||
}
|
||||
.modifier(TextFieldGrayBackgroudColor())
|
||||
Text("\($query.wrappedValue)")
|
||||
Spacer()
|
||||
List(self.locations, id: \.identifier) { location in
|
||||
Button(
|
||||
action: {
|
||||
self.findDirections(to: location)
|
||||
},
|
||||
label: {
|
||||
SearchItemView(location: location)
|
||||
})
|
||||
}
|
||||
}
|
||||
.padding()
|
||||
.interactiveDismissDisabled()
|
||||
|
||||
|
||||
.presentationDetents([.height(200), .large])
|
||||
.presentationBackground(.regularMaterial)
|
||||
.presentationBackgroundInteraction(.enabled(upThrough: .large))
|
||||
}
|
||||
|
||||
|
||||
func findDirections(to place: MKMapItem) {
|
||||
let directionsRequest = MKDirections.Request()
|
||||
// directionsRequest.source = MKMapItem.forCurrentLocation()
|
||||
directionsRequest.source = MKMapItem.init(
|
||||
placemark: MKPlacemark(
|
||||
coordinate: CLLocationCoordinate2D(latitude: 52.3676, longitude: 4.9041)))
|
||||
directionsRequest.destination = place
|
||||
directionsRequest.transportType = .walking
|
||||
directionsRequest.requestsAlternateRoutes = false // TODO: make alternative routes available
|
||||
directionsRequest.departureDate = .now
|
||||
|
||||
let searchDirections = MKDirections(request: directionsRequest)
|
||||
searchDirections.calculate { (response, error) in
|
||||
guard let response = response else {
|
||||
print(error)
|
||||
print("Error while searching for directions")
|
||||
return
|
||||
}
|
||||
for route in response.routes {
|
||||
// extract route(s)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
var items: [MKMapItem] = []
|
||||
for item in response.mapItems {
|
||||
if let name = item.name,
|
||||
let location = item.placemark.location {
|
||||
print("\(name): \(location.coordinate.latitude),\(location.coordinate.longitude)")
|
||||
let location = item.placemark.location
|
||||
{
|
||||
print(
|
||||
"\(name): \(location.coordinate.latitude),\(location.coordinate.longitude)")
|
||||
items.append(item)
|
||||
}
|
||||
}
|
||||
self.locations = items
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user