获取gps信息

(一)plist修改

添加如下变量

(二)新建视图用来启动Gps

此视图控制器继承CLLocationManagerDelegate

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@interface GpsViewController : UIViewController <CLLocationManagerDelegate>

@end

(三)定义标签用来显示位置,并开启定位

UILabel *latitudeValue;
UILabel *longitudeValue;
CLLocationManager *locationManager;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor=[UIColor redColor];
    self.view.alpha=0.5;
    
    UILabel *latitude=[[UILabel alloc] init];
    latitude.text=@"Latitude:";
    latitude.frame=CGRectMake(100, 200, 100, 15);
    [self.view addSubview:latitude];
    
    latitudeValue=[[UILabel alloc] init];
    latitudeValue.text=@"";
    latitudeValue.frame=CGRectMake(210, 200, 150, 15);
    [self.view addSubview:latitudeValue];
    
    UILabel *longitude=[[UILabel alloc] init];
    longitude.text=@"Longitude:";
    longitude.frame=CGRectMake(100, 300, 100, 15);
    [self.view addSubview:longitude];
    
    longitudeValue=[[UILabel alloc] init];
    longitudeValue.text=@"";
    longitudeValue.frame=CGRectMake(210, 300, 150, 15);
    [self.view addSubview:longitudeValue];
    
    //开启定位
    locationManager=[[CLLocationManager alloc] init];
    locationManager.delegate=self;
    locationManager.desiredAccuracy=kCLLocationAccuracyBest;
    locationManager.distanceFilter=kCLDistanceFilterNone;
    locationManager.allowsBackgroundLocationUpdates=YES;
    //使用期间定位
    [locationManager requestWhenInUseAuthorization];
    [locationManager requestAlwaysAuthorization];
    [locationManager startUpdatingLocation];
// Do any additional setup after loading the view.
}

(四)通过委托说去实时位置

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
    NSLog(@"Update location");
    CLLocation *newLoaction=locations[0];
    latitudeValue.text=[[NSString alloc] initWithFormat:@"%f",newLoaction.coordinate.latitude];
    longitudeValue.text=[[NSString alloc] initWithFormat:@"%f",newLoaction.coordinate.longitude];
}

(五)用户权限检测

    if ([CLLocationManager locationServicesEnabled]) {
        switch ([CLLocationManager authorizationStatus]) {
            case kCLAuthorizationStatusNotDetermined:
                NSLog(@"用户尚未进行选择");
                break;
            case kCLAuthorizationStatusRestricted:
                NSLog(@"定位权限被限制");
                break;
            case kCLAuthorizationStatusAuthorizedAlways:
            case kCLAuthorizationStatusAuthorizedWhenInUse:
                NSLog(@"用户允许定位");
                break;
            case kCLAuthorizationStatusDenied:
                NSLog(@"用户不允许定位");
                break;
                
            default:
                break;
        }
    }

(六)定位失败委托

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"Update error");
    
    if(error.code == kCLErrorLocationUnknown) {
        NSLog(@"无法检索位置");
    }
    else if(error.code == kCLErrorNetwork) {
        NSLog(@"网络问题");
    }
    else if(error.code == kCLErrorDenied) {
        NSLog(@"定位权限的问题");
        [locationManager stopUpdatingLocation];
    }
}

猜你喜欢

转载自www.cnblogs.com/llstart-new0201/p/9693105.html