繁体   English   中英

使用CLLocationManager获取用户位置时出错

[英]Error when getting user location with CLLocationManager

我正在使用集合视图和集合视图可重用标头。 在标题中,我试图获取用户的位置。 我不断收到此错误:

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

我看着这个问题: 定位服务在iOS 8中不起作用,并且所有提议的解决方案都不适合我。 我已将密钥(NSLocationAlwaysUsageDescription和NSLocationWhenInUseUsageDescription)添加到我的info.Plist中,但这没有帮助。 该应用程序从不要求用户获取其位置。 这是我的代码:

编辑:我的代码现在:

   import UIKit
   import MapKit
   import Parse
   import ParseUI
   import CoreLocation

   class ReusableHeaderMap: UICollectionReusableView, CLLocationManagerDelegate {
let locationManager = CLLocationManager()

@IBOutlet weak var newMap: MKMapView!


override func awakeFromNib() {
    locationManager.delegate = self
    let status = CLLocationManager.authorizationStatus()
    if status == CLAuthorizationStatus.Denied {

    }
    else if status == CLAuthorizationStatus.NotDetermined {
        self.locationManager.requestAlwaysAuthorization()
    }
    else if status == CLAuthorizationStatus.AuthorizedAlways {
        self.locationManager.startUpdatingLocation()
    }
    else {
        print("it took the else route")

    }

    print("status is \(status)")
    self.newMap.layer.cornerRadius = 8
    self.newMap.clipsToBounds = true

}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    if locations.count > 0 {
        locationManager.stopUpdatingLocation()
        let location1 = locations.first as CLLocation!
        let mapSpan = MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05)
        let mapRegion = MKCoordinateRegion(center: location1.coordinate, span: mapSpan)
        self.newMap.setRegion(mapRegion, animated: true)
    }
}


func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {

    if status == CLAuthorizationStatus.Denied {
        print("there will be limited functionaliy")
    }
    else if status == CLAuthorizationStatus.AuthorizedAlways {
        self.locationManager.startUpdatingLocation()
    }
    else {
        print("it took the else route")
      }
   }


}

我的info.plist具有此密钥:

  <key>NSLocationAlwaysUsageDescription</key>
<string>Get User Location</string>

错误消息告诉您您必须执行的操作。 您需要打电话给

self.locationManager.requestWhenInUseAuthorization() 

要么

self.locationManager.requestAlwaysAuthorization()

在尝试启动位置管理器之前,请先输入代码。 该步骤是将NSLocationAlwaysUsageDescription和/或NSLocationWhenInUseUsageDescription键添加到info.plist的NSLocationWhenInUseUsageDescription

有关更多信息,请参见有关这些方法的文档。

(查看您发布的代码,您没有正确设置内容。您不能只调用requestWhenInUseAuthorization然后在之后立即startUpdatingLocation 。)

正如文档所说:

当前授权状态为kCLAuthorizationStatusNotDetermined时,此方法异步运行,并提示用户向应用授予使用位置服务的权限。

那里的关键词是异步的

您需要进行该调用,然后实现locationManager:didChangeAuthorizationStatus:方法。 如果用户授予您的应用使用位置服务的权限,则将调用该方法(didChangeAuthorizationStatus)。

您的代码应如下所示:

override func awakeFromNib() 
{
  locationManager.delegate = self
  let status = locationManager.status
  if (status == kCLAuthorizationStatusNotDetermined)
  {
    locationManager.requestWhenInUseAuthorization()
  }
  else if (status == kCLAuthorizationStatusAuthorized)
  {
    locationManager.startUpdatingLocation()
  }
  else
  {
    //Handle other error cases here.
  }
  newMap.layer.cornerRadius = 8
  newMap.clipsToBounds = true
}

optional func locationManager(_ manager: CLLocationManager,
  didChangeAuthorizationStatus status: CLAuthorizationStatus)
{
  locationManager.startUpdatingLocation()
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM