-
Notifications
You must be signed in to change notification settings - Fork 208
Expand file tree
/
Copy pathMapView.swift
More file actions
119 lines (106 loc) 路 3.34 KB
/
Copy pathMapView.swift
File metadata and controls
119 lines (106 loc) 路 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//
// MapView.swift
// Example
//
// Created by Matheus Gois on 12/06/24.
//
import SwiftUI
import MapKit
import CoreLocation
@available(iOS 14.0, *)
struct MapView: View {
@Environment(\.presentationMode) var presentationMode
@State private var region = MKCoordinateRegion(
center: CLLocationCoordinate2D(latitude: 51.507222, longitude: -0.1275),
span: MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5)
)
private var manager = MapViewManager()
var body: some View {
NavigationView {
Group {
if #available(iOS 17.0, *) {
MapView17(manager: manager)
} else {
Map(
coordinateRegion: $region,
showsUserLocation: true,
userTrackingMode: .constant(.follow)
)
.onAppear {
manager.start()
}
}
}
.navigationTitle("Map")
.navigationBarTitleDisplayMode(.inline)
.navigationBarItems(
leading: Button("Done") {
presentationMode.wrappedValue.dismiss()
}
)
}
}
}
@available(iOS 17.0, *)
struct MapView17: View {
@State private var position = MapCameraPosition.region(
MKCoordinateRegion(
center: CLLocationCoordinate2D(latitude: 51.507222, longitude: -0.1275),
span: MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5)
)
)
var manager: MapViewManager
var body: some View {
Map(position: $position) {
// MapContentBuilder content can be added here
}
.mapControls {
MapUserLocationButton()
MapCompass()
MapScaleView()
}
.onAppear {
manager.start()
}
}
}
@available(iOS 14.0, *)
class MapViewManager: NSObject, CLLocationManagerDelegate {
@State private var locationManager = CLLocationManager()
func start() {
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
switch status {
case .authorizedWhenInUse, .authorizedAlways:
manager.startUpdatingLocation()
case .denied, .restricted:
break
case .notDetermined:
manager.requestWhenInUseAuthorization()
@unknown default:
fatalError("Unhandled case for location authorization status")
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("""
-----
NEW LOCATION IS UPDATED, TO SHOW IN MAP, NEEDS RESTART THE APP
\(locations)
-----
""")
}
}
@available(iOS 14.0, *)
extension MapViewManager: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
let region = MKCoordinateRegion(
center: userLocation.coordinate,
latitudinalMeters: 10000,
longitudinalMeters: 10000
)
mapView.setRegion(region, animated: true)
}
}