我一直在尝试将我的iOS7应用与MKMapview一起移动以支持iOS8。但是,我无法获得新的要求,要求用户共享其位置才能正常工作。我在情节提要板上创建了MKMapView,并设置了委托并可以在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;
}
另外,我NSLocationAlwaysUsageDescription
还在我的InfoPlist中设置了键及其值,当提示用户共享位置时,该键会显示正确的消息。
不幸的是,委托函数-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
从未被调用过。尽管每次加载viewController时都会[self.locationManager startUpdatingLocation]
调用,但是委托似乎没有响应。我该如何设置委托人是否存在问题,或者我在这里还缺少其他东西?
更新:似乎我的gpx文件也没有在启动时被调用。我已经清除并重新加载了位置文件,甚至更改为默认位置,但未找到任何位置:Domain=kCLErrorDomain Code=0
更新2:这是我实际上已成功完成用户请求的设置中的SS,但是无论刷新多少,都无法获取/更新位置。(来源:barisaltop.com)
谢谢!
几天前我遇到了同样的问题。解决方案是将(for )或(for )string
键添加到Supporting Files / Info.plistNSLocationAlwaysUsageDescription
[CLLocationManager requestAlwaysAuthorization]
NSLocationWhenInUseUsageDescription
[CLLocationManager requestWhenInUseAuthorization]
您还可以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>
希望这可以帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句