定位

1.添加plist里授权描述


2.创建管理对象

- (CLLocationManager *)locationManager {

    if (_locationManager != nil) {

        return _locationManager;

    }

    _locationManager = [[CLLocationManager alloc] init];

    [_locationManager setDelegate:self];

    _locationManager.distanceFilter = 5.0;

    [_locationManager setDesiredAccuracy:kCLLocationAccuracyBest];

    return _locationManager;

}

3.请求授权

- (void)updateLocation {

    int status = [CLLocationManager authorizationStatus];

    if (status < 3) {

        if ([[UIDevice currentDevice].systemVersion floatValue] >= 8) {

            [self.locationManager requestAlwaysAuthorization];

            [self.locationManager requestWhenInUseAuthorization];

        }

    }else {

        [self.locationManager startUpdatingLocation];

    }

}

#pragma mark CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {

    if (status < 3) {

        NSLog(@"未授权");

    }else {

        [_locationManager startUpdatingLocation];

    }

}

4.获取定位

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {

    NSLog(@"定位成功!");

    [manager stopUpdatingLocation];

    _location = locations.firstObject;

    NSLog(@"%f %f",_location.coordinate.latitude,_location.coordinate.longitude);

}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{

    NSLog(@"定位失败!");

}

你可能感兴趣的:(定位)