热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

iOS8和Swift中的MapKit-MapKitiniOS8andSwift

ImtryingtouseMapKitoniOS8andIkeepgettingtheerror:我正在尝试在iOS8上使用MapKit并且我不断收到错误:Try

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. What am I doing wrong?

在这里查看,我发现我必须在我的plist中实现NSLocationWhenInUsageDescription,并且还调用了locationManager.requestWhenInUseAuthorization(),但没有任何反应,我仍然在控制台中得到该错误。我究竟做错了什么?

3 个解决方案

#1


10  

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. I then set

然后在视图控制器文件中,我在类外部创建了另一个var来保存本地CLLocationManager。我接着说

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.

仅当您在使用时获得授权时,才会将用户位置添加到地图中。

#2


9  

I have been bothered by that one as well, until I realized that the info.plist key has changed. 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.

我也被那个人困扰,直到我意识到info.plist键已经改变。如果您有NSLocationUsageDescription,则需要更改为NSLocationAlwaysUsageDescription或NSLocationWhenInUseUsageDescription(将至​​少一个设置为YES),现在它在iOS8中是必需的。

And then, Robert's code works, as it should (thanks for sharing).

然后,罗伯特的代码正常工作(感谢分享)。

#3


7  

It is NSLocationWhenInUseUsageDescription, not NSLocationWhenInUsageDescription. Most places online have the wrong key

它是NSLocationWhenInUseUsageDescription,而不是NSLocationWhenInUsageDescription。网上的大多数地方都有错误的密钥


推荐阅读
author-avatar
萌新求学
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有