feat(search): search for a location and get directions to it
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
# StepMap
|
# StepMap
|
||||||
## TODO:
|
## TODO:
|
||||||
- [ ] Create a map
|
- [x] 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
|
||||||
- [ ] Get walkingStepLength from HealthKit
|
- [ ] 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.
|
// Created by Oliver Hnát on 23.11.2024.
|
||||||
//
|
//
|
||||||
|
|
||||||
import SwiftUI
|
|
||||||
import MapKit
|
import MapKit
|
||||||
|
import SwiftUI
|
||||||
|
|
||||||
struct SearchView: View {
|
struct SearchView: View {
|
||||||
@State private var query: String = ""
|
@State private var query: String = ""
|
||||||
|
@State private var locations: [MKMapItem] = []
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
VStack {
|
VStack {
|
||||||
HStack {
|
HStack {
|
||||||
@@ -21,8 +23,16 @@ struct SearchView: View {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
.modifier(TextFieldGrayBackgroudColor())
|
.modifier(TextFieldGrayBackgroudColor())
|
||||||
Text("\($query.wrappedValue)")
|
|
||||||
Spacer()
|
Spacer()
|
||||||
|
List(self.locations, id: \.identifier) { location in
|
||||||
|
Button(
|
||||||
|
action: {
|
||||||
|
self.findDirections(to: location)
|
||||||
|
},
|
||||||
|
label: {
|
||||||
|
SearchItemView(location: location)
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
.padding()
|
.padding()
|
||||||
.interactiveDismissDisabled()
|
.interactiveDismissDisabled()
|
||||||
@@ -32,6 +42,30 @@ struct SearchView: View {
|
|||||||
.presentationBackgroundInteraction(.enabled(upThrough: .large))
|
.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) {
|
func search(for text: String) {
|
||||||
let searchRequest = MKLocalSearch.Request()
|
let searchRequest = MKLocalSearch.Request()
|
||||||
searchRequest.naturalLanguageQuery = text
|
searchRequest.naturalLanguageQuery = text
|
||||||
@@ -42,12 +76,17 @@ struct SearchView: View {
|
|||||||
print("ERROR")
|
print("ERROR")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
var items: [MKMapItem] = []
|
||||||
for item in response.mapItems {
|
for item in response.mapItems {
|
||||||
if let name = item.name,
|
if let name = item.name,
|
||||||
let location = item.placemark.location {
|
let location = item.placemark.location
|
||||||
print("\(name): \(location.coordinate.latitude),\(location.coordinate.longitude)")
|
{
|
||||||
|
print(
|
||||||
|
"\(name): \(location.coordinate.latitude),\(location.coordinate.longitude)")
|
||||||
|
items.append(item)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
self.locations = items
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user