我在我的应用程序中使用 MKMapView
并且它工作正常。我可以通过 didUpdate userLocation 方法正确获取更新后的位置。
我的问题是这样的:
Instance method 'mapView(_:didFailToLocateUserWithError:)' nearly matches optional requirement 'mapView(_:didFailToLocateUserWithError:)' of protocol 'MKMapViewDelegate'
注释:
我已经编写了如下方法:
func mapView(_ mapView: MKMapView, didFailToLocateUserWithError error: Error) { //code }
我已经实现了正确的协议(protocol):
MKMapViewDelegate
。didUpdateUserLocation
方法工作正常。我使用的是 Xcode 9.2,部署目标:10.0,Swift 3.2。
我在方法签名处添加了@nonobjc,并且没有出现警告,但该方法从未执行。
最佳答案
确保已添加此内容
mapView.showsUserLocation = true
并检查授权
let locationManager = CLLocationManager()
if CLLocationManager.authorizationStatus() == .notDetermined {
locationManager.requestWhenInUseAuthorization()
} else if CLLocationManager.authorizationStatus() == .authorizedWhenInUse {
mapView.showsUserLocation = true
} else {
//show alert
}
关于ios - 实例方法 'mapView(_:didFailToLocateUserWithError:)'几乎符合可选要求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48323243/