This article turns from: Http://blog.devzeng.com/blog/ios8-corelocation-framework.htmliOS8 previously used corelocation positioning
1, first define a global variable used to record the Cllocationmanager object, the introduction of the CoreLocation.framework use#import <CoreLocation/CoreLocation.h>
@property (nonatomic, strong) Cllocationmanager *locationmanager;
2. Initialize the Cllocationmanager and start positioning
self.locationManager = [[CLLocationManager alloc]init];_locationManager.delegate = self;_locationManager.desiredAccuracy = kCLLocationAccuracyBest;_locationManager.distanceFilter = 10;[_locationManager startUpdatingLocation];
3, the implementation of Cllocationmanagerdelegate agent method
(1) Gets the location data, returns an array of cllocation, typically using one of the
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{ CLLocation *currLocation = [locations lastObject]; NSLog(@"经度=%f 纬度=%f 高度=%f", currLocation.coordinate.latitude, currLocation.coordinate.longitude, currLocation.altitude);}
(2) A callback method to get the user's location data failed, which notifies the user
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{ if ([error code] == kCLErrorDenied) { //访问被拒绝 } if ([error code] == kCLErrorLocationUnknown) { //无法获取位置信息 }}
4, viewWillDisappear close the positioning
- (void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; [_locationManager stopUpdatingLocation];}
Using corelocation positioning in iOS8
1, before using corelocation need to call the following function "IOS8 dedicated":
IOS8 has made some modifications to the positioning, including the method of locating authorization, Cllocationmanager adds the following two methods:
(1) Always allow access to location information
- (void)requestAlwaysAuthorization;
(2) Allow access to location data during use of the application
- (void)requestWhenInUseAuthorization;
Examples are as follows:
self.locationManager = [[CLLocationManager alloc]init];_locationManager.delegate = self;_locationManager.desiredAccuracy = kCLLocationAccuracyBest;_locationManager.distanceFilter = 10;[_locationManager requestAlwaysAuthorization];//添加这句[_locationManager startUpdatingLocation];
2. Add the following configuration in the Info.plist file:
(1)NSLocationAlwaysUsageDescription
(2)NSLocationWhenInUseUsageDescription
The value of these two keys is the description of the authorization alert, and the example is configured as follows [ 勾选Show Raw Keys/Values后进行添加 ]:
Resources
1, "Meet the Change of Ios8–corelocation"
2. "Core location of iOS development"
3, "IOS8 under the position of authorization"
Use corelocation to locate [turn] in iOS8