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);
}
原文地址:https://www.cnblogs.com/gchlcc/p/5843959.html