ios - iOS 8 和 Swift 中的 MapKit

标签 ios mapkit swift ios8

我试图在 iOS 8 上使用 MapKit,但我一直收到错误:

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

在这里查找,我发现我必须在我的 plist 中实现 NSLocationWhenInUsageDescription 并调用 locationManager.requestWhenInUseAuthorization() 但什么也没有发生,我仍然在控制台中得到该错误。我做错了什么?

最佳答案

在我的应用程序委托(delegate)中,我为类外的 locationManager 创建了一个可选的 var,然后设置

locManager = CLLocationManager()
locManager!.requestWhenInUseAuthorization()

这会导致警报 View 弹出 NSLocationWhenInUseUsageDescription 或 NSLocationAlwaysUsageDescription 如果你适本地改变它。

然后在 View Controller 文件中,我在类外创建了另一个 var 来保存本地 CLLocationManager。然后我设置

if locManager {
        locMan = locManager!
        locMan!.delegate = self
    }

然后就可以使用delegate方法了

func locationManager(_manager: CLLocationManager!,didChangeAuthorizationStatus status: CLAuthorizationStatus)

当授权状态改变时调用,当用户响应弹出窗口时调用。在此内部,您可以使用这段代码将用户位置放在 map 上

if status == CLAuthorizationStatus.AuthorizedWhenInUse {
        map.showsUserLocation = true
    }

只有在您被授权使用时才会将用户位置添加到 map 中。

关于ios - iOS 8 和 Swift 中的 MapKit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24105896/

相关文章:

ios - swift : TableView Cells not getting selected correctly

ios - 使用 Youtube API 下载字幕

ios - unity 5 "Build & Install iPhone player"对话框不消失

ios - UISearchBar 位于 UICollectionView 之上,如何让它仅与集合中的项目一起工作?

ios - 自定义 UIActivity 的主题

ios - 100,000 多个 MapKit 注释

ios - 字符串和属性问题

ios - VectorKit MapKit 堆栈溢出崩溃

iphone - 如何从地址确定 CLLocation 或 MKPlacemark?

ios - 查询不工作 Firebase Swift