Trying to get the map to center on the current location. It is showing "Updated Location" over and over again, it then doesn't change the map the current location. If you scroll to your current location, it shows the blue dot.
struct Home:View {
@State var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 13.086, longitude: 80.2769), latitudinalMeters: 10000, longitudinalMeters: 10000)
@State var trackingMode: MapUserTrackingMode = .follow
@State var manager = CLLocationManager()
@StateObject var managerDelegate = LocationDelegate()
var body:some View {
VStack {
Map(coordinateRegion: $region, interactionModes: .all, showsUserLocation: true, userTrackingMode: $trackingMode)
}
.onAppear {
manager.delegate = managerDelegate
}
}
}
class LocationDelegate: NSObject, ObservableObject, CLLocationManagerDelegate {
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
if manager.authorizationStatus == .authorizedWhenInUse {
print("Authorized...")
manager.startUpdatingLocation()
} else {
print("Not Authorized...")
manager.requestWhenInUseAuthorization()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("Updated Location")
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…