iOS地图 -- 定位中的CLLocation的介绍与小练习

通过定位练习,熟悉CLLocation

  • 在上篇笔记中提到了CLLocation类,这里通过练习来讲解一下这个类,类中包含了获取到的用户位置的信息
    • coordinate --> 坐标,经度和纬度
    • altitude --> 海拔
    • horizontalAccuracy --> 水平精度
    • verticalAccuracy -->垂直精度
    • course --> 航向
    • speed --> 速度
    • timestamp --> 时间戳
    • distanceFromLocation: --> 计算两个位置之间的距离
  • 练习要求: 打印:北偏东 30度方向 走了100米
    位置管理者的懒加载什么的就不在这里写了
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
    CLLocation *location = [locations lastObject];
    // 打印:北偏东 30度方向 走了100米

    // 1.计算方向
    NSArray *arr = @[@"北偏东",@"东偏南",@"南偏西",@"西偏北"];
    int index = (int)(location.course / 90); // course航向
    NSString *direction = arr[index];
    // 2.计算度数
    int degree = (int)location.course % 90;
    if (degree == 0) {
        direction = [@"正" stringByAppendingString:[direction substringToIndex:1]];
    }
    // 3.计算路程
    double distance = 0;
    if (self.preivousLoc) {

        distance = [location distanceFromLocation:self.preivousLoc]; // 计算两点之间的距离
    }
    self.preivousLoc = location;
    // 4.拼串
    NSString *result;
    if (degree != 0) {

        result = [NSString stringWithFormat:@"%@ %zd度方向 走了%f米",direction,degree,distance];
    }
    else {

        result = [NSString stringWithFormat:@"%@ 方向 走了%f米",direction,distance];
    }
    NSLog(@"%@",result);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值