首页 > 移动平台 > 详细

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

时间:2015-10-21 15:45:52      阅读:729      评论: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

(1)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!