[英]Getting user's current location iOS 8.0
我试图使用MapKit和CoreLocation获取用户当前位置。 我真的是Objective-C的新手。 就我的研究而言,其实现与旧版iOS到iOS 8.0略有不同。 我正确地遵循了所有内容。 仍然它正在获取当前位置。 我的实际目标是在运行时,当用户在文本字段中给出位置时,在mapKit上获取用户的位置。
filename.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface LocationSelectionViewController : UIViewController <CLLocationManagerDelegate>
@property (weak, nonatomic) IBOutlet UITextField *insertLocation;
@property (strong, nonatomic) IBOutlet MKMapView *serviceLocation;
- (IBAction)next:(id)sender;
@end
#import "LocationSelectionViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface LocationSelectionViewController () <CLLocationManagerDelegate>
@end
@implementation LocationSelectionViewController
CLLocationManager *locationManager;
@synthesize serviceLocation;
- (void)viewDidLoad {
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager requestAlwaysAuthorization];
[locationManager startUpdatingLocation];
// Do any additional setup after loading the view.
self.serviceLocation = [[MKMapView alloc] initWithFrame:self.view.bounds];
self.serviceLocation.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:self.serviceLocation];
[self.serviceLocation.delegate self];
//[self.serviceLocation setShowsUserLocation:YES];
}
/*- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
CLLocationCoordinate2D loc=[userLocation coordinate];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 100, 100);
[self.serviceLocation setRegion:region animated:YES];
}*/
-(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
[locationManager requestAlwaysAuthorization];
[locationManager startUpdatingLocation];
}
-(void) locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
if(status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse){
self.serviceLocation.showsUserLocation = YES;
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
- (IBAction)next:(id)sender {
}
@end
如图所示,我根据iOS 8的要求添加了两个属性。
谁能建议我将其存档:
您请求和开始位置更新的流程不正确。 首先, requestAlwaysAuthorization
不会立即更改授权状态,因为它需要用户按警报中的“ Allow
或Don't Allow
按钮。 因此,您应该仅在获得结果后才开始位置更新。 然后,在didUpdateLocations
无需再次请求/重新启动。
按照以下方式进行
在您的viewDidLoad
BOOL isAuthorized = [CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedAlways;
if (isAuthorized) {
[locationManager startUpdatingLocation];
}
else {
[locationManager requestAlwaysAuthorization];
}
并实现类似的委托方法
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
CLLocation *current = locations.firstObject;
}
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
if(status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse){
self.serviceLocation.showsUserLocation = YES;
[locationManager startUpdatingLocation];
}
}
在要请求权限的iOS 8中,您应该在info.plist
添加用法描述字符串。 在您的情况下,键为NSLocationAlwaysUsageDescription
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.