feat(UIKit): create a UIKit map and use that instead of swiftui map
This commit is contained in:
BIN
StepMap.xcodeproj/project.xcworkspace/xcuserdata/oliverhnat.xcuserdatad/UserInterfaceState.xcuserstate
generated
Normal file
BIN
StepMap.xcodeproj/project.xcworkspace/xcuserdata/oliverhnat.xcuserdatad/UserInterfaceState.xcuserstate
generated
Normal file
Binary file not shown.
@@ -42,16 +42,19 @@ struct ContentView: View {
|
|||||||
// calculate only the distance between the start and end instead of getting directions for everything
|
// 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
|
// if user clicks on the place, display better view and then calculate route there
|
||||||
var body: some View {
|
var body: some View {
|
||||||
Map(position: $position) {
|
ZStack {
|
||||||
UserAnnotation()
|
MapView(locationManager: locationManager)
|
||||||
ForEach(0..<directions.count) { i in
|
// Map(position: $position) {
|
||||||
if destination != nil {
|
// UserAnnotation()
|
||||||
Marker(item: destination!)
|
// ForEach(0..<directions.count) { i in
|
||||||
}
|
// if destination != nil {
|
||||||
MapPolyline(directions[i].polyline)
|
// Marker(item: destination!)
|
||||||
.stroke(Defaults.routeColor[i], lineWidth: Defaults.routeWidth)
|
// }
|
||||||
}
|
// MapPolyline(directions[i].polyline)
|
||||||
|
// .stroke(Defaults.routeColor[i], lineWidth: Defaults.routeWidth)
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
.ignoresSafeArea()
|
||||||
.sheet(
|
.sheet(
|
||||||
isPresented: $showSearch,
|
isPresented: $showSearch,
|
||||||
content: {
|
content: {
|
||||||
|
|||||||
77
StepMap/UIKitMapView.swift
Normal file
77
StepMap/UIKitMapView.swift
Normal 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
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user