diff --git a/README.md b/README.md index 324124d..3ebf4f6 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/StepMap/SearchItemView.swift b/StepMap/SearchItemView.swift new file mode 100644 index 0000000..865e650 --- /dev/null +++ b/StepMap/SearchItemView.swift @@ -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 ?? "")") + } +} diff --git a/StepMap/SearchView.swift b/StepMap/SearchView.swift index 371f920..7e11fe9 100644 --- a/StepMap/SearchView.swift +++ b/StepMap/SearchView.swift @@ -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 } } }