Fix map centering bug

This commit is contained in:
Garth Vander Houwen 2023-03-19 19:32:33 -07:00
parent a991087bc2
commit ca302cbcd5
2 changed files with 19 additions and 15 deletions

View file

@ -47,7 +47,7 @@ class UserSettings: ObservableObject {
}
@Published var meshMapRecentering: Bool {
didSet {
UserDefaults.standard.set(meshMapCenteringMode, forKey: "meshMapRecentering")
UserDefaults.standard.set(meshMapRecentering, forKey: "meshMapRecentering")
UserDefaults.standard.synchronize()
}
}

View file

@ -138,25 +138,29 @@ struct MapViewSwiftUI: UIViewRepresentable {
let annotationCount = waypoints.count + positions.count
if annotationCount != mapView.annotations.count {
mapView.removeAnnotations(mapView.annotations)
let latest = positions.filter { $0.latest == true }
mapView.addAnnotations(waypoints)
mapView.setUserTrackingMode(userTrackingMode, animated: true)
if userTrackingMode != MKUserTrackingMode.none {
mapView.showsUserLocation = true
} else {
if userTrackingMode == MKUserTrackingMode.none {
mapView.showsUserLocation = false
}
switch centeringMode {
case .allAnnotations:
mapView.addAnnotations(positions)
if recenter && userTrackingMode == MKUserTrackingMode.none {
mapView.fitAllAnnotations()
}
case .allPositions:
if recenter && userTrackingMode == MKUserTrackingMode.none {
mapView.fit(annotations: positions, andShow: true)
} else {
switch centeringMode {
case .allAnnotations:
mapView.addAnnotations(positions)
if recenter && userTrackingMode == MKUserTrackingMode.none {
mapView.fitAllAnnotations()
}
case .allPositions:
if recenter && userTrackingMode == MKUserTrackingMode.none {
mapView.fit(annotations: positions, andShow: true)
} else {
mapView.addAnnotations(positions)
}
}
} else {
// Centering Done by tracking mode
mapView.addAnnotations(positions)
mapView.showsUserLocation = true
}
}
}