wip
This commit is contained in:
parent
528e474d91
commit
c3679c9a2a
|
@ -1,79 +1,42 @@
|
||||||
|
import Combine
|
||||||
import CoreLocation
|
import CoreLocation
|
||||||
import MapKit
|
import MapKit
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
struct AttachmentLocationPickerView: View {
|
struct AttachmentLocationPickerView: View {
|
||||||
@StateObject var locationManager = LocationManager()
|
@State var region = MKCoordinateRegion()
|
||||||
@State private var region = MKCoordinateRegion()
|
@State var userTrackingMode: MapUserTrackingMode = .follow
|
||||||
@State var userInteracted: Bool = false
|
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
|
ZStack {
|
||||||
Map(
|
Map(
|
||||||
coordinateRegion: $region,
|
coordinateRegion: $region,
|
||||||
interactionModes: .all,
|
interactionModes: .all,
|
||||||
showsUserLocation: false,
|
showsUserLocation: false,
|
||||||
userTrackingMode: .none
|
userTrackingMode: $userTrackingMode
|
||||||
)
|
)
|
||||||
.onAppear {
|
|
||||||
locationManager.start()
|
|
||||||
}
|
|
||||||
.onDisappear {
|
|
||||||
locationManager.stop()
|
|
||||||
}
|
|
||||||
.onChange(of: locationManager.region) { region in
|
|
||||||
if !userInteracted {
|
|
||||||
let currentLoc = CLLocation(latitude: self.region.center.latitude, longitude: self.region.center.longitude)
|
|
||||||
let newLoc = CLLocation(latitude: region.center.latitude, longitude: region.center.longitude)
|
|
||||||
if newLoc.distance(from: currentLoc) > 10 {
|
|
||||||
DispatchQueue.main.async {
|
|
||||||
withAnimation {
|
|
||||||
self.region = region
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
.overlay {
|
.overlay {
|
||||||
Image(systemName: "mappin")
|
Image(systemName: "mappin")
|
||||||
.font(.title2)
|
.resizable()
|
||||||
|
.frame(width: 30, height: 30)
|
||||||
.foregroundColor(.Material.Elements.active)
|
.foregroundColor(.Material.Elements.active)
|
||||||
}
|
if userTrackingMode != .follow {
|
||||||
.onChange(of: region) { region in
|
VStack {
|
||||||
print("Region changed: \(region.center)")
|
Spacer()
|
||||||
|
HStack {
|
||||||
|
Spacer()
|
||||||
|
Image(systemName: "location.north.circle.fill")
|
||||||
|
.resizable()
|
||||||
|
.frame(width: 30, height: 30)
|
||||||
|
.foregroundColor(.Material.Elements.active)
|
||||||
|
.padding()
|
||||||
|
.tappablePadding(.symmetric(10)) {
|
||||||
|
userTrackingMode = .follow
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class LocationManager: NSObject, ObservableObject, CLLocationManagerDelegate {
|
|
||||||
@Published var region = MKCoordinateRegion()
|
|
||||||
private let locationManager = CLLocationManager()
|
|
||||||
|
|
||||||
override init() {
|
|
||||||
super.init()
|
|
||||||
locationManager.delegate = self
|
|
||||||
locationManager.desiredAccuracy = kCLLocationAccuracyBest
|
|
||||||
}
|
|
||||||
|
|
||||||
func start() {
|
|
||||||
locationManager.requestWhenInUseAuthorization()
|
|
||||||
locationManager.startUpdatingLocation()
|
|
||||||
}
|
|
||||||
|
|
||||||
func stop() {
|
|
||||||
locationManager.stopUpdatingLocation()
|
|
||||||
}
|
|
||||||
|
|
||||||
func locationManager(_: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
|
|
||||||
guard let location = locations.last else { return }
|
|
||||||
region = MKCoordinateRegion(center: location.coordinate, span: MKCoordinateSpan(latitudeDelta: 0.001, longitudeDelta: 0.001))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue