SwiftUI error - "Delegate must respond to locationManager:didFailWithError:"
SwiftUI error - "Delegate must respond to locationManager:didFailWithError:"
我正在尝试在 SwiftUI 中实现 MapKit 和 CoreLocation 以在我的应用程序中获取用户位置、检查权限、处理错误等。但是当我 运行 模拟器并授予位置权限。
不确定我做错了什么。感谢任何帮助
Here's a screenshot with where the error message shows
这是我的代码,我认为它缺少导致错误的内容:
import SwiftUI
import MapKit
import CoreLocation
// All Map data goes here
class MapViewModel: NSObject,ObservableObject, CLLocationManagerDelegate{
@Published var mapView = MKMapView()
// Region...
@Published var region : MKCoordinateRegion!
// Based on location it will set up...
// Alert...
@Published var permissionDenied = false
// Map Type...
@Published var mapType : MKMapType = .standard
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
// Checking permissions...
switch manager.authorizationStatus {
case .denied:
// Alert...
permissionDenied.toggle()
case .notDetermined:
// Requesting...
manager.requestWhenInUseAuthorization()
case .authorizedWhenInUse:
// If permission given...
manager.requestLocation()
default:
()
}
}
func locationManager(_manager: CLLocationManager, didFailWithError error: Error) {
// Error...
print(error.localizedDescription)
}
// Getting user Region...
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations:
[CLLocation]) {
guard let location = locations.last else {return}
self.region = MKCoordinateRegion(center: location.coordinate, latitudinalMeters: 10000, longitudinalMeters: 10000)
// Updating Map...
self.mapView.setRegion(self.region, animated: true)
// Smooth animation...
self.mapView.setVisibleMapRect(self.mapView.visibleMapRect, animated: true)
}
}
func locationManager(_manager: CLLocationManager,
应该是
func locationManager(_ manager: CLLocationManager,
我正在尝试在 SwiftUI 中实现 MapKit 和 CoreLocation 以在我的应用程序中获取用户位置、检查权限、处理错误等。但是当我 运行 模拟器并授予位置权限。
不确定我做错了什么。感谢任何帮助
Here's a screenshot with where the error message shows
这是我的代码,我认为它缺少导致错误的内容:
import SwiftUI
import MapKit
import CoreLocation
// All Map data goes here
class MapViewModel: NSObject,ObservableObject, CLLocationManagerDelegate{
@Published var mapView = MKMapView()
// Region...
@Published var region : MKCoordinateRegion!
// Based on location it will set up...
// Alert...
@Published var permissionDenied = false
// Map Type...
@Published var mapType : MKMapType = .standard
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
// Checking permissions...
switch manager.authorizationStatus {
case .denied:
// Alert...
permissionDenied.toggle()
case .notDetermined:
// Requesting...
manager.requestWhenInUseAuthorization()
case .authorizedWhenInUse:
// If permission given...
manager.requestLocation()
default:
()
}
}
func locationManager(_manager: CLLocationManager, didFailWithError error: Error) {
// Error...
print(error.localizedDescription)
}
// Getting user Region...
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations:
[CLLocation]) {
guard let location = locations.last else {return}
self.region = MKCoordinateRegion(center: location.coordinate, latitudinalMeters: 10000, longitudinalMeters: 10000)
// Updating Map...
self.mapView.setRegion(self.region, animated: true)
// Smooth animation...
self.mapView.setVisibleMapRect(self.mapView.visibleMapRect, animated: true)
}
}
func locationManager(_manager: CLLocationManager,
应该是
func locationManager(_ manager: CLLocationManager,