这是我的代码,显示 map 上当前位置的警报和蓝点:
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;
@end
map 名称.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 上仍然遇到同样的错误。
最佳答案
这是由于:
[self.locationManager startUpdatingLocation];
和
_MapName.showsUserLocation = YES;
在调用这些之前,您需要检查用户是否已授予权限。还要确保关闭 Storyboard 上 MKMapKit 中的用户位置(这花了我几天时间才找到)。
做类似的事情:
CLAuthorizationStatus authorizationStatus= [CLLocationManager authorizationStatus];
if (authorizationStatus == kCLAuthorizationStatusAuthorized ||
authorizationStatus == kCLAuthorizationStatusAuthorizedAlways ||
authorizationStatus == kCLAuthorizationStatusAuthorizedWhenInUse) {
[self.locationManager startUpdatingLocation];
_MapName.showsUserLocation = YES;
}
根据您的应用,您可能不想在启动时请求用户的许可,因为不建议这样做。
关于iphone - -[CLLocationManager requestWhenInUseAuthorization] 或 -[CLLocationManager requestAlwaysAuthorization] 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25950363/