我一直在尝试使用 MKMapview 移动我的 iOS7 应用程序以支持 iOS8。但是,我无法获得让用户共享其位置以正常工作的新请求。我在 Storyboard 上创建了我的 MKMapView,委托(delegate)已设置并在 iOS7 上完美运行。以下是我为支持 iOS8 位置共享而添加的内容:
myMapView.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface myMapView : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate>
@property (strong, nonatomic) IBOutlet MKMapView *mapView;
@property (strong, nonatomic) CLLocationManager *locationManager;
myMapView.m
//Code omitted
#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
//Code omitted
- (void)viewDidLoad {
[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
if(IS_OS_8_OR_LATER) {
//[self.locationManager requestWhenInUseAuthorization];
[self.locationManager requestAlwaysAuthorization];
[self.locationManager startUpdatingLocation];
}
[self.mapView setShowsUserLocation:YES];
[self.mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
MKCoordinateRegion region = { { 0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = self.locationManager.location.coordinate.latitude;
region.center.longitude = self.locationManager.location.coordinate.longitude;
region.span.latitudeDelta = 0.0187f;
region.span.longitudeDelta = 0.0137f;
[self.mapView setRegion:region animated:YES];
_initialPosition = NO;
}
此外,我还在我的 InfoPlist 中设置了 NSLocationAlwaysUsageDescription
键及其值,这在提示用户共享位置时显示了正确的消息。
不幸的是,委托(delegate)函数 -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
永远不会被调用。虽然每次加载 viewController 时都会调用 [self.locationManager startUpdatingLocation]
,但委托(delegate)似乎没有响应它。我如何设置委托(delegate)有问题还是我在这里缺少其他东西?
更新:我的 gpx 文件似乎也没有在启动时被调用。我已经清除并重新加载我的位置文件,甚至更改为默认位置,但没有找到位置:Domain=kCLErrorDomain Code=0
更新 2: 这是来自设置的 SS,我实际上已成功满足用户请求,但无论我刷新多少次都无法获取/更新位置。
(来源:barisaltop.com)
谢谢!
最佳答案
几天前我遇到了同样的问题。解决方案是添加 string
键 NSLocationAlwaysUsageDescription
(对于 [CLLocationManager requestAlwaysAuthorization]
)或 NSLocationWhenInUseUsageDescription
(对于 [CLLocationManager requestWhenInUseAuthorization]
) 添加到您的Supporting Files/Info.plist
您还可以编辑 Info.Plist
的源代码,方法是右键单击 > 打开为 > 源代码 并添加以下行:
<!-- for requestAlwaysAuthorization -->
<key>NSLocationAlwaysUsageDescription</key>
<string>Explain for what are you using the user location</string>
<!-- for requestWhenInUseAuthorization -->
<key>NSLocationWhenInUseUsageDescription</key>
<string>Explain for what are you using the user location</string>
希望这对您有所帮助。
关于iOS 8 MKMapView 用户定位请求失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25848125/