获取用户的当前位置iOS 8.0

我试图让用户使用MapKit和CoreLocation的当前位置。 我是Objective-C的新手。 至于我的研究,这个实现和旧的iOS到iOS 8.0稍有不同。 我一切正确。 仍然是获得当前的位置。 我的实际目标是在用户在文本字段中提供位置时,在运行时获取pinkit用户位置。

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的要求添加了两个属性。

图片

任何人都可以build议我归档这个:

  • 获取用户当前的位置。
  • 在运行时从文本字段中获取位置并locking在MapKip上。

您请求和开始位置更新的stream程不正确。 首先, requestAlwaysAuthorization不会立即更改授权状态,因为它需要用户按下警报中的AllowDon't Allowbutton。 所以你应该在得到结果之后才开始位置更新。 然后在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添加使用说明string。 在你的情况下,关键是NSLocationAlwaysUsageDescription