get location by corelocation

1.plist

Privacy - Location When In Use Usage Description
Privacy - Location Always Usage Description

2.header

#import 
#import 

3.init

- (void)initLocation {
    self.locationManager=[[CLLocationManager alloc] init];
    self.locationManager.delegate=self;
    self.locationManager.desiredAccuracy=kCLLocationAccuracyBest;
    self.locationManager.distanceFilter=10;
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
        [self.locationManager requestWhenInUseAuthorization];
    }
    [self.locationManager startUpdatingLocation];
}

4.delegate

- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray *)locations{
    CLLocation *location=[locations firstObject];
    CLLocationCoordinate2D coordinate=location.coordinate;
    NSLog(@"lat:%f,lon:%f",coordinate.longitude,coordinate.latitude);
}

- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error{
    if ([error code] == kCLErrorDenied) {
        NSLog(@"reject");
    }
    if ([error code] == kCLErrorLocationUnknown) {
        NSLog(@"failed");
    }
}

5.Postscript

any question please comment below,i will response you.

你可能感兴趣的:(get location by corelocation)