gpt4 book ai didi

swift - 尝试在不提示位置授权的情况下启动 MapKit 位置更新

转载 作者:搜寻专家 更新时间:2023-10-31 22:35:34 24 4
gpt4 key购买 nike

在我的应用程序中,我使用 MKMapKitMKUserTrackingBarButtonItem 来定位点击的用户。当我点击这个按钮时,输出控制台返回这个错误:

Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.

根据我的说法,此错误是由于 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 。

我的问题是:如何在 MKUserTrackingBarButtonItem 点击但在 mapViewWillStartLocatingUser 启动之前调用 CLLocationManager requestWhenInUseAuthorization。我希望用户在点击按钮时得到提示,而 viewDidLoad

中没有

谢谢对不起我的英语

最佳答案

当您调用 requestWhenInUseAuthorization 时,系统会提示用户提供权限,但该权限尚未获得授权。一旦用户授予该权限,该问题将不会再次出现。

要对用户的响应使用react,您必须实现locationManager(_:didChangeAuthorizationStatus:) 来自 CLLocationManagerDelegate 协议(protocol),只有在用户授予权限时才启动 startUpdateLocations()

关于swift - 尝试在不提示位置授权的情况下启动 MapKit 位置更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29396679/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com