在我的应用程序中,我使用MKMapKit和MKUserTrackingBarButtonItem来定位用户。当我点击此按钮时,输出控制台将返回此错误:
据我说,造成此错误的原因是还没有调用requestWhenInUseAuthorization()。实际上,MKUserTrackingBarButtonItem Tap会先调用功能性mapViewWillStartLocatingUser,该功能先假定CLLocationManager requestWhenInUseAuthorization:
func mapViewWillStartLocatingUser(mapView: MKMapView!) {
println("**** mapViewWillStartLocatingUser ****")
// i servizi di localizzazione sono abilitati?
if (CLLocationManager.locationServicesEnabled())
{
// setto il locationManager ed il delegate
locationManager = CLLocationManager()
locationManager.delegate = self
// abbiamo l'autorizzazione ad accedere ai servizi di localizzazione?
switch CLLocationManager.authorizationStatus(){
case .Denied:
// no
displayAlertToEnableLocationServicesApp()
//displayAlertWithTitle("Denied", message: "Location services are not allowed for this app")
case .Restricted:
// no
displayAlertToEnableLocationServicesApp()
case .NotDetermined:
// bisogna chiedere all'utente
println("Not Determined")
if (locationManager != nil)
{
locationManager.requestWhenInUseAuthorization()
}
default:
// si
println("Authorized")
if (locationManager != nil)
{
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
}
}
else
{
println("Location services are not enabled")
displayAlertWithTitle("Location Services are Turned Off", message: "Please open settings and turn on location services")
}
}
// funzione della mappa
func mapView(mapView: MKMapView!, didFailToLocateUserWithError error: NSError!) {
println("**** didFailToLocateUserWithError **** ", error)
}
// funzione della mappa
func mapView(mapView: MKMapView!, didChangeUserTrackingMode mode: MKUserTrackingMode, animated: Bool) {
println("**** didChangeUserTrackingMode ****")
}
// funzione del CoreLocation che setta la visuale in base alla localizzaizone dell'utente
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
println("**** didUpdateLocations ****")
//self.mapView.showsUserLocation = true
// aggiorno le coordinate dell'utente
posizioneUtente = (locations[0] as CLLocation).coordinate
//posizioneUtente = manager.location.coordinate
println("Posizione utente aggiornata (lat: \(posizioneUtente.latitude) long: \(posizioneUtente.longitude))")
// setto la camera sulla posizione dell'utente
var camera = MKMapCamera(lookingAtCenterCoordinate: posizioneUtente, fromEyeCoordinate: posizioneUtente, eyeAltitude: 500)
// utilizzo un'animazione più lenta
UIView.animateWithDuration(1.8, animations:{
self.mapView.camera = camera
})
locationManager.stopUpdatingLocation()
// cambio l'icona del bottone della localizzazione
//locationOutlet.setImage(UIImage(named: "LocalizzazioneEmpty"), forState: UIControlState.Normal)
}
// funzione del CoreLocation
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!)
{
println("**** didFailWithError ****")
println("Error: \(error.localizedDescription)")
}
// funzione del CoreLocation
func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
print("The authorization status of location " + "services is changed to: ")
switch CLLocationManager.authorizationStatus(){
case .Denied:
println("Denied")
case .NotDetermined:
println("Not determined")
case .Restricted:
println("Restricted")
default:
println("Authorized")
}
}
我自然地添加了Info.plist键:NSLocationWhenInUseUsageDescription。
我的问题是:在启动mapViewWillStartLocatingUser之前,如何在MKUserTrackingBarButtonItem水龙头上调用CLLocationManager requestWhenInUseAuthorization。我希望用户在点击按钮时应获得提示,而在viewDidLoad中则不显示
谢谢
对不起我的英语不好
最佳答案
当您调用requestWhenInUseAuthorization
时,如果该权限尚未得到授权,则系统会提示用户该权限。用户授予该权限后,该问题将不再出现。
要对用户的响应使用react,您必须实现locationManager(_:didChangeAuthorizationStatus:)
协议(protocol)中的CLLocationManagerDelegate
,并且只有在用户授予许可的情况下才启动startUpdateLocations()
。
关于swift - 尝试启动MapKit位置更新而没有提示位置授权,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29396679/