这是我的代码,显示了地图上当前位置的警报和蓝点:
MapName.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface MapName : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate>
@property (strong, nonatomic) IBOutlet MKMapView *MapName;
@property (strong, nonatomic) CLLocationManager *locationManager;
@endMapName.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc]init];
self.locationManager.delegate = self;
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[self.locationManager requestWhenInUseAuthorization];
}
[self.locationManager startUpdatingLocation];
//Center the map
[self gotoLocation];
//Show current position
_MapName.showsUserLocation = YES;
}我已经将键NSLocationWhenIsUseUsageDescription作为字符串添加到Info.plist中。我在Xcode上仍然会遇到同样的错误。
发布于 2014-10-01 23:24:22
其原因是:
[self.locationManager startUpdatingLocation];和
_MapName.showsUserLocation = YES;在调用这些文件之前,您需要检查用户是否已授予权限。还要确保关闭故事板上的MKMapKit中的用户位置(这一次花了我几天的时间追踪)。
做以下事情:
CLAuthorizationStatus authorizationStatus= [CLLocationManager authorizationStatus];
if (authorizationStatus == kCLAuthorizationStatusAuthorized ||
authorizationStatus == kCLAuthorizationStatusAuthorizedAlways ||
authorizationStatus == kCLAuthorizationStatusAuthorizedWhenInUse) {
[self.locationManager startUpdatingLocation];
_MapName.showsUserLocation = YES;
}根据您的应用程序,您可能不想要求用户的许可启动,因为这是不推荐的。
发布于 2014-09-22 16:25:18
错误消息是非常字面的。在获得授权之前,不要调用[self.locationManager startUpdatingLocation]。根据文档,您的[self.locationManager requestWhenInUseAuthorization]是异步的。
当当前授权状态为
kCLAuthorizationStatusNotDetermined时,此方法异步运行,并提示用户授予应用程序使用位置服务的权限。
这意味着,您既提示进行访问,又同时启动扫描。
相反,尝试实现-[CLLocationManagerDelegate locationManager:didChangeAuthorizationStatus:]并在确定有授权后在那里启动扫描。
发布于 2016-03-31 10:22:43
关于迅捷的:
let locationManager: CLLocationManager = CLLocationManager()
let authorizationStatus = CLLocationManager.authorizationStatus()
override func viewDidLoad() {
super.viewDidLoad()
if(authorizationStatus == .AuthorizedWhenInUse || authorizationStatus == .AuthorizedAlways) {
locationManager.startUpdatingLocation()
}
else
{
locationManager.requestWhenInUseAuthorization()
}
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
}不要忘记将以下键添加到Info.plist文件中:

https://stackoverflow.com/questions/25950363
复制相似问题