获取用户的当前位置iOS 8.0 [英] Getting user's current location iOS 8.0

查看:101
本文介绍了获取用户的当前位置iOS 8.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用MapKit和CoreLocation获取用户当前位置。我是Objective-C的新手。至于我的研究,实现与旧版iOS到iOS 8.0略有不同。我已经正确地遵循了一切。它仍然是当前的位置。我的实际目标是在用户在文本字段中给出位置时,在运行时获取mapKit上的pin用户位置。

I trying to get users current location using MapKit and CoreLocation. I'm really new to Objective-C. As for my research the implementation is slightly different from older iOS to iOS 8.0. I have followed everything correctly. Still it is getting current location. My actual target is to get pin user location on the mapKit during run time when the user gives the location in the text field.

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

As in the image I have added the two properties as per iOS 8 requirement.

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

Can anyone advise me in archiving this:


  • 获取用户当前位置。

  • 从文本字段获取位置并固定在MapKip上在运行期间。

推荐答案

您的请求和开始位置更新的流程不正确。首先, requestAlwaysAuthorization 不会立即更改授权状态,因为它需要用户按允许不要在警报中允许按钮。因此,您应该在获得结果后才开始更新位置。然后在 didUpdateLocations 中无需再次请求/重新启动。

Your flow for requesting and starting location update is not proper. First of all the requestAlwaysAuthorization will not change the authorization status immediately because it needs user to press Allow or Don't Allow button in the alert. So you should start location update only after getting its result. Then in didUpdateLocations no need to request/start again.

按以下方式执行,

你的 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

In iOS 8 for requesting permission you should add the usage description string in the info.plist. In your case the key is NSLocationAlwaysUsageDescription.

这篇关于获取用户的当前位置iOS 8.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆