feat(location): enable user location input
This commit is contained in:
@@ -255,6 +255,8 @@
|
||||
DEVELOPMENT_TEAM = SSJBLTMP95;
|
||||
ENABLE_PREVIEWS = YES;
|
||||
GENERATE_INFOPLIST_FILE = YES;
|
||||
INFOPLIST_KEY_NSLocationAlwaysAndWhenInUseUsageDescription = "We need to access your location to set starting point of your route";
|
||||
INFOPLIST_KEY_NSLocationWhenInUseUsageDescription = "We need to access your location to set starting point of your route";
|
||||
INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;
|
||||
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
|
||||
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
|
||||
@@ -284,6 +286,8 @@
|
||||
DEVELOPMENT_TEAM = SSJBLTMP95;
|
||||
ENABLE_PREVIEWS = YES;
|
||||
GENERATE_INFOPLIST_FILE = YES;
|
||||
INFOPLIST_KEY_NSLocationAlwaysAndWhenInUseUsageDescription = "We need to access your location to set starting point of your route";
|
||||
INFOPLIST_KEY_NSLocationWhenInUseUsageDescription = "We need to access your location to set starting point of your route";
|
||||
INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;
|
||||
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
|
||||
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
|
||||
|
||||
@@ -5,11 +5,13 @@
|
||||
// Created by Oliver Hnát on 23.11.2024.
|
||||
//
|
||||
|
||||
import CoreLocation
|
||||
import MapKit
|
||||
import SwiftUI
|
||||
|
||||
struct ContentView: View {
|
||||
@ObservedObject var viewModel = ViewModel()
|
||||
@StateObject var locationManager = LocationManager()
|
||||
|
||||
@State private var position = MapCameraPosition.automatic
|
||||
@State private var showSearch: Bool = true
|
||||
@@ -33,9 +35,29 @@ struct ContentView: View {
|
||||
.sheet(
|
||||
isPresented: $showSearch,
|
||||
content: {
|
||||
SearchView(directions: $directions)
|
||||
SearchView(directions: $directions, locationManager: locationManager)
|
||||
.ignoresSafeArea()
|
||||
})
|
||||
}
|
||||
)
|
||||
.mapControls {
|
||||
MapUserLocationButton()
|
||||
.onTapGesture {
|
||||
locationManager.requestAuthorization()
|
||||
locationManager.requestLocation()
|
||||
if let userLocation = locationManager.location {
|
||||
position = .camera(
|
||||
MapCamera(centerCoordinate: userLocation, distance: 1000))
|
||||
}
|
||||
}
|
||||
}
|
||||
.onAppear {
|
||||
locationManager.requestAuthorization()
|
||||
locationManager.requestLocation()
|
||||
if let userLocation = locationManager.location {
|
||||
position = .camera(MapCamera(centerCoordinate: userLocation, distance: 1000))
|
||||
}
|
||||
|
||||
}
|
||||
// Text("This is what's set: \(viewModel.test)")
|
||||
// Button(action: {
|
||||
// save(value: "te5t3")
|
||||
|
||||
57
StepMap/LocationManager.swift
Normal file
57
StepMap/LocationManager.swift
Normal file
@@ -0,0 +1,57 @@
|
||||
//
|
||||
// LocationManager.swift
|
||||
// StepMap
|
||||
//
|
||||
// Created by Oliver Hnát on 25.11.2024.
|
||||
//
|
||||
|
||||
import CoreLocation
|
||||
import Foundation
|
||||
|
||||
class LocationManager: NSObject, ObservableObject, CLLocationManagerDelegate {
|
||||
let manager = CLLocationManager()
|
||||
|
||||
@Published var location: CLLocationCoordinate2D?
|
||||
|
||||
override init() {
|
||||
super.init()
|
||||
manager.delegate = self
|
||||
}
|
||||
|
||||
func requestLocation() {
|
||||
manager.requestLocation()
|
||||
}
|
||||
|
||||
func requestAuthorization() {
|
||||
manager.requestWhenInUseAuthorization()
|
||||
}
|
||||
|
||||
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
|
||||
location = locations.first?.coordinate
|
||||
}
|
||||
}
|
||||
|
||||
extension LocationManager {
|
||||
func locationManager(
|
||||
_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus
|
||||
) {
|
||||
switch status {
|
||||
case .notDetermined:
|
||||
print("Authorization not determined")
|
||||
case .restricted:
|
||||
print("Authorization restricted")
|
||||
case .denied:
|
||||
print("Authorization denied")
|
||||
case .authorizedAlways:
|
||||
print("Authorization always authorized")
|
||||
case .authorizedWhenInUse:
|
||||
print("Authorization when in use authorized")
|
||||
@unknown default:
|
||||
print("Authorization went wrong")
|
||||
}
|
||||
}
|
||||
|
||||
func locationManager(_ manager: CLLocationManager, didFailWithError error: any Error) {
|
||||
print(error)
|
||||
}
|
||||
}
|
||||
@@ -74,10 +74,10 @@ struct SearchItemView: View {
|
||||
|
||||
func findDirections() {
|
||||
let directionsRequest = MKDirections.Request()
|
||||
// directionsRequest.source = MKMapItem.forCurrentLocation()
|
||||
directionsRequest.source = MKMapItem.init(
|
||||
placemark: MKPlacemark(
|
||||
coordinate: CLLocationCoordinate2D(latitude: 52.3676, longitude: 4.9041)))
|
||||
directionsRequest.source = MKMapItem.forCurrentLocation()
|
||||
// directionsRequest.source = MKMapItem.init(
|
||||
// placemark: MKPlacemark(
|
||||
// coordinate: CLLocationCoordinate2D(latitude: 52.3676, longitude: 4.9041)))
|
||||
directionsRequest.destination = location
|
||||
directionsRequest.transportType = .walking
|
||||
directionsRequest.requestsAlternateRoutes = false // TODO: make alternative routes available
|
||||
|
||||
@@ -12,6 +12,7 @@ struct SearchView: View {
|
||||
@State private var query: String = "airport"
|
||||
@State private var locations: [MKMapItem] = []
|
||||
@Binding var directions: [MKRoute]
|
||||
var locationManager: LocationManager
|
||||
|
||||
var body: some View {
|
||||
VStack {
|
||||
@@ -43,28 +44,6 @@ struct SearchView: View {
|
||||
.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
|
||||
}
|
||||
self.directions = response.routes
|
||||
}
|
||||
}
|
||||
|
||||
func search(for text: String) {
|
||||
let searchRequest = MKLocalSearch.Request()
|
||||
searchRequest.naturalLanguageQuery = text
|
||||
|
||||
Reference in New Issue
Block a user