iOS定位-核心定位框架CLLocation

原文链接:biggergao.github.io/CLLocation

我是前言

最近做了一下CLLocation相关的东西,较全面的写了点相关问题与解决办法,文章的demo可以在这里下载(有点RAC的知识)。
如果是模拟器,运行时请选择Location GPX文件
祝学习愉快。

副本主要任务

  • 定位设备经纬度与所在城市

预备知识-CLLocation对象(可跳过)

CLLocation对象存储着CLLocationManager对象生成的位置数据,先介绍一下它的属性大概了解CLLocation是什么东西

用于定位的属性含义
coordinate地理位置(经纬度)
altitude海拔
floor建筑内逻辑楼层
timestamp定位时间戳
horizontalAccuracy水平技能范围,单位米(见注1)
verticalAccuracy海拔误差,单位米

注1:我们在地图上的点由经度和纬度确定,horizontalAccuracy表示该圆的半径是多大(单位为米),负值表示该点无效(经常用在if语句中判断点是否可用)

用于速度和方向的属性含义
speed瞬时速度
course设备移动方向

实战

1.模拟器参数设置(可跳过)

1.1添加GPX文件设置

修改latitude(经度)和longitude(纬度)的值,可以使用图上的值lat=”29.568863”和lon=”106.460922”,美丽山城重庆。

最后调试选择对应的GPX文件即可

1.2直接修改模拟器的值

修改参数即可

2.获取经纬度

2.1 iOS8前的BUG

我们需要在info.plist文件里添加两个字段给APP定位权限,不然在iOS8后是无法启动定位的。他们分别是

属性名含义
NSLocationWhenInUseUsageDescription使用期间
NSLocationAlwaysUsageDescription始终开启

添加如下:

上个效果图好理解点:

2.2核心代码讲解
- (void)findCurrentLocation {
    self.isFirstUpdate = YES;
    // 1
    if (! [CLLocationManager locationServicesEnabled]) {
        [TSMessage showNotificationWithTitle:@"未开启定位服务"
                                    subtitle:@"请开启定位服务定位您所在城市."
                                        type:TSMessageNotificationTypeError];
    }
    // 2
    else if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
        [self.locationManager requestWhenInUseAuthorization];
        [self.locationManager startUpdatingLocation];
    }
    // 3
    else {
        [self.locationManager requestAlwaysAuthorization];
        [self.locationManager startUpdatingLocation];
    }
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
    if (self.isFirstUpdate) {
        // 4
        self.isFirstUpdate = NO;
        return;
    }

    // 5
    CLLocation *newLocation = [locations lastObject];

    self.currentLocation = newLocation;

    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    // 反向地理编译出地址信息
    [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        if (! error) {
            if ([placemarks count] > 0) {
                CLPlacemark *placemark = [placemarks firstObject];

                // 获取城市
                NSString *city = placemark.locality;
                if (! city) {
                    // 6
                    city = placemark.administrativeArea;
                }

                self.currentCity = city;
            } else if ([placemarks count] == 0) {
                [TSMessage showNotificationWithTitle:@"GPS故障"
                                            subtitle:@"定位城市失败"
                                                type:TSMessageNotificationTypeError];
            }
        } else {
            [TSMessage showNotificationWithTitle:@"网络错误"
                                        subtitle:@"请检查您的网络"
                                            type:TSMessageNotificationTypeError];
        }
    }];
    [self.locationManager stopUpdatingLocation];
}

1、未开启定位服务

2、使用时定位

3、始终定位

4、第一次数据可以是久值,需舍弃

5、locations中有两个元素,第一个为旧值,第二个为新值

6、四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市)

最后通关副本:

参考博客:

http://blog.csdn.net/ndscoahz/article/details/42418729

http://blog.it985.com/13173.html

Done

作者 @biggergao

2016年05月15日

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值