美文网首页iOS开发iOS Developer
iOS 关于定位的那点事.

iOS 关于定位的那点事.

作者: KumLight | 来源:发表于2016-11-15 15:02 被阅读150次

最近在整理之前运用到的一些技术 , 关于定位 我完善了一下知识体系 , 今天跟大家分享一下 关于定位 CLLocationManager 的那些事 .


基本配置

  1. 导入架包 #import <CoreLocation/CoreLocation.h>
  2. 创建对象
//这边需要注意的是 要用strong 强引用
@property (nonatomic, strong) CLLocationManager  *locationManager;
  1. 声明代理 <CLLocationManagerDelegate>

  2. plist 文件配置


    配置请求定位服务许可
  3. 如果要后台定位,需要打开后台模式


    勾选后台模式
  4. 代码实现

    // 判断是否打开了位置服务
    if ([CLLocationManager locationServicesEnabled]) {
        
        self.locationManager = [[CLLocationManager alloc] init];
        self.locationManager.delegate = self;

        //设置定位距离过滤参数 (当本次定位和上次定位之间的距离大于或等于这个值时,调用代理方法)
        self.locationManager.distanceFilter = 20;

        //设置定位精度(精度越高越耗电)
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;

        //请求定位服务许可
        if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
            [self.locationManager requestWhenInUseAuthorization];
        } else {
            [self.locationManager requestAlwaysAuthorization];
        }

        //开始更新位置
        [self.locationManager startUpdatingLocation];
        
    }
//iOS9 之后 必须实现的两个代理方法
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{

    NSLog(@"locations : %@",locations);
    
    if (locations) {
        
        CLLocation *currentLocation = [locations lastObject];
        //经度
        NSNumber *latitude = [NSNumber numberWithDouble:
                              currentLocation.coordinate.latitude];
        //纬度
        NSNumber *longitude = [NSNumber numberWithDouble:
                               currentLocation.coordinate.longitude];
        
        [self.locationManager stopUpdatingLocation];
    }
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{

    NSLog(@"Error : %@",error);

}

** CLLocation 属性**

  1. coordinate : 当前位置的坐标
  • latitude : 纬度
  • longitude : 经度
  1. altitude : 海拔,高度
  2. horizontalAccuracy : 纬度和经度的精度
  3. verticalAccuracy : 垂直精度(获取不到海拔时为负数)
  4. course : 行进方向(真北)
  5. speed : 以米/秒为单位的速度
  6. description : 位置描述信息

定位城市

- (void)getLocationCity:(CLLocation *)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) {
                    city = placemark.administrativeArea;
                }
                
                self.currentCity.text = city;
                
            }
        }
    }];
}

** CLPlacemark 属性**

  1. name:地址名称
  2. country:国家
  3. administrativeArea:省份(行政区)
  4. locality:所属城市
  5. subLocality:所属城市中的区、县等
  6. thoroughfare:带街道的地址名称
  7. subThoroughfare : 街道号
  8. timeZone:时区

计算两点之间的距离

//调用计算两位置之前距离的方法 (单位为 米)
- (CLLocationDistance)getDistanceFromlocation:(CLLocation *)location
                            ToAnotherLocation:(CLLocation *)anotherLocation{

    CLLocationDistance meters = [location distanceFromLocation:anotherLocation];
    
    return meters;
    
}

参考文档 :
http://www.jianshu.com/p/ef6994767cbb
https://segmentfault.com/a/1190000004563937

相关文章

  • iOS 关于定位的那点事.

    最近在整理之前运用到的一些技术 , 关于定位 我完善了一下知识体系 , 今天跟大家分享一下 关于定位 CLLoc...

  • iOS时间那点事--NSDateFormatter

    文章出处 iOS时间那点事--NSDate iOS时间那点事--NSDateFormatter iOS时间那点事-...

  • iOS开发:设计模式那点事

    iOS开发:设计模式那点事 iOS开发:设计模式那点事

  • 关于阅读的那点事定位

    最近听得最多的一句话就是谁谁谁去参加阅读营了,然后就凭着一腔热情兴致勃勃的去参加,可是没有坚持几天就退缩了,因为她...

  • 疯话连篇:晋文抄袭笔记(42)

    关于营销那点儿事: 1,定位。定位利用的是庸众两大心理:迷信权威(XX行业领导者)追求从众随大流(销量遥遥领先) ...

  • 关于ios登录的那点事

    #pragmamark - TextFieldDelegate -(BOOL)textFieldShouldBeg...

  • iOS / UITextField

    iOS开发-UITextField的那点事 - 简书 iOS10 自动填充手机号实现UITextContentTy...

  • 关于iOS添加udid那点事

    今天公司老大叫我添加了一个udid,然后再打一个包给他。我在apple developer上添加了udid,但是忘...

  • IOS 关于GIF图片那点事

    前言 前几天我们项目组的群里提了这么一件事情:在我们的应用中存储动态的GIF图到相册,保存的图片变成了静态图片。而...

  • iOS开发关于证书的那点事

    讲之前先介绍一下bundle id,这个东西就相当于我们每个人日常生活中的身份证一样,一个App有且是有唯一的bu...

网友评论

    本文标题:iOS 关于定位的那点事.

    本文链接:https://www.haomeiwen.com/subject/vjlkpttx.html