实例方法' mapView(_:didFailToLocateUserWithError :)'几乎匹配可选要求

时间:2018-01-18 14:09:32

标签: ios swift delegates mapkit

我在我的应用中使用MKMapView并且它正常运行。我可以通过didUpdate userLocation方法正确获取更新的位置。

我的问题是:

  

实例方法' mapView(_:didFailToLocateUserWithError :)'几乎匹配可选要求' mapView(_:didFailToLocateUserWithError :)'协议' MKMapViewDelegate'

备注

  • 我编写的方法完全如下:

    func mapView(_ mapView: MKMapView, didFailToLocateUserWithError error: Error) {
        //code
    }
    
  • 我已经实施了正确的协议:MKMapViewDelegate

  • 方法didUpdateUserLocation正常运作。

  • 我正在使用Xcode 9.2,部署目标:10.0,Swift 3.2。

  • 我在方法签名处添加了@nonobjc,并且没有出现警告,但该方法永远不会执行。

1 个答案:

答案 0 :(得分:0)

确保添加

 mapView.showsUserLocation = true

并检查授权

let locationManager = CLLocationManager()
if CLLocationManager.authorizationStatus() == .notDetermined {
  locationManager.requestWhenInUseAuthorization()
} else if CLLocationManager.authorizationStatus() ==   .authorizedWhenInUse {
   mapView.showsUserLocation = true
} else {
   //show alert
}