码迷,mamicode.com
首页 > 移动开发 > 详细

iOS开发-定位服务iOS8.0以上授权

时间:2015-10-21 15:47:10      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:

if ([UIDevice currentDevice].systemVersion.doubleValue > 8.0) {

        [self.locMgr requestAlwaysAuthorization];

    } else {

        [self.locMgr startUpdatingLocation];

    }

 

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

    if (status == kCLAuthorizationStatusNotDetermined) {

        NSLog(@"等待用户授权");

    }

    if (status == kCLAuthorizationStatusAuthorizedWhenInUse || status == kCLAuthorizationStatusAuthorizedAlways) {

        // 得到用户授权开始定位

        [self.locMgr startUpdatingLocation];

        

    } else {

        NSLog(@"授权失败");

    }

}

/**

 *  当定位到用户的位置时,就会调用(调用的频率比较频繁)

 */

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

 

    // locations 数组里存放的是 CLLocation 对象, 一个 CLLocation 对象就代表着一个位置

    CLLocation *loc = [locations firstObject];

    // 纬度 : loc.coordinate.latitude

    // 经度 : loc.coordinate.longitude

    NSLog(@"纬度:%f, 经度:%f",loc.coordinate.latitude,loc.coordinate.longitude);

    

    

    CLLocation *c = [[CLLocation alloc] initWithLatitude:loc.coordinate.latitude longitude:loc.coordinate.longitude];

    //创建位置

    CLGeocoder *revGeo = [[CLGeocoder alloc] init];

    [revGeo reverseGeocodeLocation:c completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {

        if (!error && [placemarks count] > 0) {

            NSDictionary *dict = [[placemarks objectAtIndex:0] addressDictionary];

            NSLog(@"%@",[dict objectForKey:@"Country"]);

            NSLog(@"%@",[dict objectForKey:@"State"]);

            NSLog(@"%@",[dict objectForKey:@"SubLocality"]);

            NSLog(@"%@",[dict objectForKey:@"Street"]);//  Country(国家)  State(城市)  SubLocality(区) Street(详细)

        } else {

            NSLog(@"ERROR: %@", error); }

    }];

 

    [self.locMgr stopUpdatingLocation];

}

 

iOS开发-定位服务iOS8.0以上授权

标签:

原文地址:http://www.cnblogs.com/xvewuzhijing/p/4897843.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!