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 *)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 * _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日

你可能感兴趣的:(iOS定位-核心定位框架CLLocation定位经纬度、所在城市)