2024-07-02 09:56:27 +00:00
|
|
|
import MapKit
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct AttachmentLocationPickerView: View {
|
2024-07-05 08:36:35 +00:00
|
|
|
@StateObject var locationManager = LocationManager()
|
2024-07-05 08:43:40 +00:00
|
|
|
@State private var region = MKCoordinateRegion()
|
2024-07-02 09:56:27 +00:00
|
|
|
|
|
|
|
var body: some View {
|
2024-07-05 08:36:35 +00:00
|
|
|
ZStack {
|
2024-07-05 08:43:40 +00:00
|
|
|
// MapView
|
2024-07-05 09:58:31 +00:00
|
|
|
MapView(region: $region)
|
|
|
|
.onAppear {
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
|
|
|
|
self.region = locationManager.region
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.overlay {
|
|
|
|
Image(systemName: "mappin")
|
|
|
|
.foregroundColor(.Material.Elements.active)
|
|
|
|
.font(.system(size: 30))
|
|
|
|
.shadow(color: .white, radius: 2)
|
|
|
|
}
|
2024-07-05 08:43:40 +00:00
|
|
|
|
|
|
|
// Track button
|
|
|
|
VStack {
|
|
|
|
Spacer()
|
|
|
|
HStack {
|
|
|
|
Spacer()
|
2024-07-05 09:58:31 +00:00
|
|
|
Image(systemName: "location.circle")
|
2024-07-05 08:43:40 +00:00
|
|
|
.resizable()
|
2024-07-05 09:58:31 +00:00
|
|
|
.frame(width: 40, height: 40)
|
2024-07-05 08:43:40 +00:00
|
|
|
.foregroundColor(.Material.Elements.active)
|
|
|
|
.background(Color.Material.Shape.white)
|
|
|
|
.clipShape(Circle())
|
|
|
|
.shadow(color: .white, radius: 2)
|
2024-07-05 09:58:31 +00:00
|
|
|
.padding(.trailing)
|
|
|
|
.padding(.bottom, 50)
|
2024-07-05 08:43:40 +00:00
|
|
|
.tappablePadding(.symmetric(10)) {
|
2024-07-05 09:58:31 +00:00
|
|
|
self.region = locationManager.region
|
2024-07-05 08:43:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-07-05 08:36:35 +00:00
|
|
|
}
|
|
|
|
.onAppear {
|
|
|
|
locationManager.start()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct MapView: UIViewRepresentable {
|
|
|
|
@Binding var region: MKCoordinateRegion
|
|
|
|
|
|
|
|
func makeUIView(context: Context) -> MKMapView {
|
|
|
|
let mapView = MKMapView()
|
|
|
|
mapView.delegate = context.coordinator
|
2024-07-05 08:43:40 +00:00
|
|
|
mapView.showsUserLocation = false
|
2024-07-05 08:36:35 +00:00
|
|
|
mapView.userTrackingMode = .none
|
2024-07-05 08:43:40 +00:00
|
|
|
|
2024-07-05 08:36:35 +00:00
|
|
|
return mapView
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateUIView(_ uiView: MKMapView, context _: Context) {
|
2024-07-05 09:58:31 +00:00
|
|
|
if uiView.region != region {
|
2024-07-05 08:36:35 +00:00
|
|
|
uiView.setRegion(region, animated: true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeCoordinator() -> Coordinator {
|
|
|
|
Coordinator(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
class Coordinator: NSObject, MKMapViewDelegate {
|
|
|
|
var parent: MapView
|
|
|
|
|
|
|
|
init(_ parent: MapView) {
|
|
|
|
self.parent = parent
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-05 08:43:40 +00:00
|
|
|
class LocationManager: NSObject, ObservableObject, CLLocationManagerDelegate {
|
2024-07-05 08:36:35 +00:00
|
|
|
private let locationManager = CLLocationManager()
|
|
|
|
@Published var region: MKCoordinateRegion
|
|
|
|
|
|
|
|
override init() {
|
|
|
|
region = MKCoordinateRegion()
|
|
|
|
super.init()
|
|
|
|
locationManager.delegate = self
|
|
|
|
locationManager.desiredAccuracy = kCLLocationAccuracyBest
|
|
|
|
}
|
|
|
|
|
|
|
|
func start() {
|
|
|
|
locationManager.requestWhenInUseAuthorization()
|
|
|
|
locationManager.startUpdatingLocation()
|
|
|
|
}
|
|
|
|
|
|
|
|
func stop() {
|
|
|
|
locationManager.stopUpdatingLocation()
|
|
|
|
}
|
|
|
|
|
|
|
|
func locationManager(_: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
|
|
|
|
if let loc = locations.first {
|
|
|
|
region = MKCoordinateRegion(
|
|
|
|
center: loc.coordinate,
|
|
|
|
span: MKCoordinateSpan(latitudeDelta: 0.002, longitudeDelta: 0.002)
|
2024-07-04 15:01:30 +00:00
|
|
|
)
|
2024-07-04 13:40:32 +00:00
|
|
|
}
|
2024-07-02 09:56:27 +00:00
|
|
|
}
|
|
|
|
}
|
2024-07-05 08:36:35 +00:00
|
|
|
|
|
|
|
extension MKCoordinateRegion: Equatable {
|
|
|
|
public static func == (lhs: MKCoordinateRegion, rhs: MKCoordinateRegion) -> Bool {
|
|
|
|
lhs.center.latitude == rhs.center.latitude &&
|
|
|
|
lhs.center.longitude == rhs.center.longitude &&
|
|
|
|
lhs.span.latitudeDelta == rhs.span.latitudeDelta &&
|
|
|
|
lhs.span.longitudeDelta == rhs.span.longitudeDelta
|
|
|
|
}
|
|
|
|
}
|