diff --git a/StepMap.xcodeproj/project.xcworkspace/xcuserdata/oliverhnat.xcuserdatad/UserInterfaceState.xcuserstate b/StepMap.xcodeproj/project.xcworkspace/xcuserdata/oliverhnat.xcuserdatad/UserInterfaceState.xcuserstate index 9964d7d..dd5d159 100644 Binary files a/StepMap.xcodeproj/project.xcworkspace/xcuserdata/oliverhnat.xcuserdatad/UserInterfaceState.xcuserstate and b/StepMap.xcodeproj/project.xcworkspace/xcuserdata/oliverhnat.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/StepMap/ContentView.swift b/StepMap/ContentView.swift index 5869eba..80a01c4 100644 --- a/StepMap/ContentView.swift +++ b/StepMap/ContentView.swift @@ -15,7 +15,7 @@ struct ContentView: View { @StateObject var locationManager = LocationManager() @StateObject var healthKitManager = HealthKitManager() - + // TODO: create a map // Add navigation to the map // after you click the navigation button, show the start and end place on the map with a tag or whatever it's called @@ -28,22 +28,13 @@ struct ContentView: View { // Add favorite locations - like home, work, etc (probably should be stored in core data tho:/) // FIX: search is bad lol - + // How to speed up? // calculate only the distance between the start and end instead of getting directions for everything // if user clicks on the place, display better view and then calculate route there var body: some View { MapView(locationManager: locationManager, viewModel: viewModel) - // Map(position: $position) { - // UserAnnotation() - // ForEach(0..() let mapView : MKMapView = { let map = MKMapView() map.showsUserTrackingButton = true @@ -34,6 +36,7 @@ class UIKitMapView: UIViewController, MKMapViewDelegate, CLLocationManagerDelega override func viewDidLoad() { super.viewDidLoad() + bindViewModel() mapView.delegate = self setMapConstraints() @@ -83,6 +86,42 @@ class UIKitMapView: UIViewController, MKMapViewDelegate, CLLocationManagerDelega self.present(searchViewConctroller, animated: true, completion: nil) } + private func refreshRoute() { + DispatchQueue.main.async { + for route in self.oldDirections { + self.mapView.removeOverlay(route.polyline) + } + self.oldDirections = self.viewModel.directions + for route in self.viewModel.directions { + self.mapView.addOverlay(route.polyline, level: .aboveRoads) + } + + if let destination = self.oldDestination { + self.mapView.removeAnnotation(destination) + } + if let destination = self.viewModel.destination { + self.oldDestination = MKMapItemAnnotation(mapItem: destination) + self.mapView.addAnnotation(self.oldDestination!) + } + } + } + + func mapView(_ mapView: MKMapView, rendererFor overlay: any MKOverlay) -> MKOverlayRenderer { + let renderer = MKGradientPolylineRenderer(overlay: overlay) + renderer.setColors(Defaults.routeColor, locations: []) + renderer.lineCap = .round + renderer.lineWidth = Defaults.routeWidth + return renderer + } + + private func bindViewModel() { + viewModel.$directions + .receive(on: DispatchQueue.main) + .sink { [weak self] _ in + self?.refreshRoute() + } + .store(in: &cancellables) + } } struct MapView: UIViewControllerRepresentable { diff --git a/StepMap/Utils.swift b/StepMap/Utils.swift index 07bc878..6f480ab 100644 --- a/StepMap/Utils.swift +++ b/StepMap/Utils.swift @@ -10,10 +10,10 @@ import MapKit import SwiftUI struct Defaults { - static let routeColor: [Color] = [ + static let routeColor: [UIColor] = [ .blue, - .red, - .yellow, +// .red, +// .yellow, ] static let routeWidth: CGFloat = 8