Use corelocation to locate [turn] in iOS8

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.