简体   繁体   English

iOS 8和Swift中的MapKit

[英]MapKit in iOS 8 and Swift

I'm trying to use MapKit on iOS 8 and I keep getting the error: 我试图在iOS 8上使用MapKit,但不断收到错误消息:

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

Looking it up here, I found that I had to implement NSLocationWhenInUsageDescription in my plist and also make a call to locationManager.requestWhenInUseAuthorization() but nothing happens and I still get that error in the console. 在这里查找时,我发现我必须在我的plist中实现NSLocationWhenInUsageDescription ,还必须调用locationManager.requestWhenInUseAuthorization()但是什么也没有发生,并且我仍然在控制台中收到该错误。 What am I doing wrong? 我究竟做错了什么?

In my application delegate I made an optional var for the locationManager outside the class and then set 在我的应用程序委托中,我为类之外的locationManager创建了一个可选的var,然后进行设置

locManager = CLLocationManager()
locManager!.requestWhenInUseAuthorization()

This causes the alert view to pop up with your NSLocationWhenInUseUsageDescription or NSLocationAlwaysUsageDescription if you change it appropriately. 如果您进行适当的更改,这将使警报视图随您的NSLocationWhenInUseUsageDescription或NSLocationAlwaysUsageDescription一起弹出。

Then in the view controller file I made another var outside the class to hold a local CLLocationManager. 然后,在视图控制器文件中,我在类外创建了另一个var,以保存本地CLLocationManager。 I then set 然后我设置

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

Then you can use the delegate method 然后可以使用委托方法

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

which gets called when the authorisation status changes, which it does when the user responds to the pop up. 授权状态更改时将调用该方法,当用户响应弹出窗口时将执行该操作。 Inside this you can use this bit of code to put the user location on the map 在其中,您可以使用以下代码在地图上放置用户位置

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

will add the users location to the map only if you are authorised for when in use. 仅在使用时授权您,才将用户位置添加到地图。

I have been bothered by that one as well, until I realized that the info.plist key has changed. 我也被那个困扰,直到我意识到info.plist键已经改变。 If you had NSLocationUsageDescription in, you will need to change to either NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription (set at least one to YES ), it is now mandatory in iOS8. 如果您有NSLocationUsageDescription ,则需要更改为NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription (至少将其设置为YES ),现在在iOS8中是必需的。

And then, Robert's code works, as it should (thanks for sharing). 然后,Robert的代码可以正常工作(感谢共享)。

It is NSLocationWhenInUseUsageDescription, not NSLocationWhenInUsageDescription. 它是NSLocationWhenInUseUsageDescription,而不是NSLocationWhenInUsageDescription。 Most places online have the wrong key 网上大多数地方输入的密码都不正确

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

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