iOS定位实现

导入框架
Xcode中添加 "CoreLocation.framework"
导入头文件
#import <CoreLocation/CoreLocation.h>
声明管理器和代理
@interface LocationViewController ()<CLLocationManagerDelegate>

@property (nonatomic, strong) CLLocationManager *locationManager;

@end
初始化管理器
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
开启定位服务 需要定位时调用currentLocation方位
- (void)currentLocation {
     /** 
      * 由于IOS8中定位的授权机制改变 需要进行手动授权
      * 获取授权认证,两个方法:
      * [self.locationManager requestWhenInUseAuthorization];
      * [self.locationManager requestAlwaysAuthorization];
      */
     if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)] {
         NSLog(@"requestAlwaysAuthorization");
         [self.locationManager requestAlwaysAuthorization];
     }

     // 开始定位,不断调用其代理方法
     [self.locationManager startUpdatingLocation];
 }
代理设置
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
     // 获取用户位置的对象
     CLLocation *location = [locations lastObject];
     CLLocationCoordinate2D coordinate = location.coordinate;
     NSLog(@"纬度:%f 经度:%f", coordinate.latitude, coordinate.longitude);

     // 停止定位
     [manager stopUpdatingLocation];
 }

 - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
     if (error.code == kCLErrorDenied) {
         // 提示用户出错原因, 可按住Option键点击 KCLErrorDenied的查看更多出错信息,可打印error.code值查找原因所在
     }
 }
注意事项
/**
 * iOS 8 添加下面的一些字段
 */
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<string>访问位置</string>
</plist>

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<string>始终访问位置</string>
</plist>

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<string>访问位置</string>
</plist>
基本实现
// MARK: - 开启定位
- (void)applicationLocationManager {
    // 检测定位功能是否开启
    if ([CLLocationManager locationServicesEnabled]) {
        if (!_locationManager) {
            self.locationManager = [[CLLocationManager alloc] init];
            if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
                [self.locationManager requestWhenInUseAuthorization];
                [self.locationManager requestAlwaysAuthorization];
            }
            // 设置代理
            [self.locationManager setDelegate:self];
            // 设置定位精度
            [self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
            // 设置距离筛选
            [self.locationManager setDistanceFilter:100];
            // 开始定位
            [self.locationManager startUpdatingLocation];
            // 设置开始识别方向
            [self.locationManager startUpdatingHeading];
        }
    }
    else {

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"

        UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:nil
                                                             message:@"您没有开启定位功能"
                                                            delegate:nil
                                                   cancelButtonTitle:@"确定"
                                                   otherButtonTitles:@"取消", nil];
        [alertView show];

#pragma clang diagnostic pop

    }
}

// MARK: - CLLocationManangerDelegate
// 定位成功以后调用
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {

    [self.locationManager stopUpdatingLocation];
    CLLocation *location = locations.lastObject;

    [self reverseGeocoder:location];
}

// MARK: - Geocoder
// 反地理编码
- (void)reverseGeocoder:(CLLocation *)currentLocation {
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {

        if (error || placemarks.count == 0) {

        }
        else {
            CLPlacemark *placemark = placemarks.firstObject;
            NSLog(@"placemark:%@", [[placemark addressDictionary] objectForKey:@"City"]);

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"

           UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"你的位置" message:[[placemark addressDictionary] objectForKey:@"City"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
           [alert show];

#pragma clang diagnostic pop

        }
    }];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值