feat(UIKit): create a UIKit map and use that instead of swiftui map

This commit is contained in:
Oliver
2025-05-13 17:58:04 +02:00
parent 17cc4190b8
commit b72ca2dd3d
3 changed files with 89 additions and 9 deletions

View File

@@ -42,16 +42,19 @@ struct ContentView: View {
// 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 {
Map(position: $position) {
UserAnnotation()
ForEach(0..<directions.count) { i in
if destination != nil {
Marker(item: destination!)
}
MapPolyline(directions[i].polyline)
.stroke(Defaults.routeColor[i], lineWidth: Defaults.routeWidth)
}
ZStack {
MapView(locationManager: locationManager)
// Map(position: $position) {
// UserAnnotation()
// ForEach(0..<directions.count) { i in
// if destination != nil {
// Marker(item: destination!)
// }
// MapPolyline(directions[i].polyline)
// .stroke(Defaults.routeColor[i], lineWidth: Defaults.routeWidth)
// }
}
.ignoresSafeArea()
.sheet(
isPresented: $showSearch,
content: {

View File

@@ -0,0 +1,77 @@
//
// UIKitMapView.swift
// StepMap
//
// Created by Oliver Hnat on 13/05/2025.
//
import UIKit
import MapKit
import SwiftUI
class UIKitMapView: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
var locationManager: LocationManager
let mapView : MKMapView = {
let map = MKMapView()
map.showsUserTrackingButton = true
return map
}()
init(locationManager: LocationManager) {
self.locationManager = locationManager
super.init(nibName: nil, bundle: nil)
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let userLocation = locations.last {
let viewRegion = MKCoordinateRegion(center: userLocation.coordinate, latitudinalMeters: 2000, longitudinalMeters: 2000)
mapView.setRegion(viewRegion, animated: true)
}
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
setMapConstraints()
setLocation()
}
private func setLocation() {
locationManager.requestAuthorization()
locationManager.requestLocation()
if let userLocation = locationManager.location {
let viewRegion = MKCoordinateRegion(center: userLocation, latitudinalMeters: 2000, longitudinalMeters: 2000)
mapView.setRegion(viewRegion, animated: true)
}
mapView.showsUserLocation = true
}
private func setMapConstraints() {
view.addSubview(mapView)
mapView.translatesAutoresizingMaskIntoConstraints = false
mapView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
mapView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
mapView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
mapView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
}
}
struct MapView: UIViewControllerRepresentable {
typealias UIViewControllerType = UIKitMapView
@StateObject var locationManager: LocationManager
func makeUIViewController(context: Context) -> UIKitMapView {
return UIKitMapView(locationManager: locationManager)
}
func updateUIViewController(_ uiViewController: UIKitMapView, context: Context) {
// pass
}
}