美文网首页
CLLocation对象

CLLocation对象

作者: 越天高 | 来源:发表于2020-05-02 16:27 被阅读0次

获取地图某一位置的坐标
可以用百度 进入开发者平台坐标拾取系统http://api.map.baidu.com/lbsapi/getpoint/index.html

// CLLocation
  coordinate: 经纬度信息
  altitude: 海拔信息
  horizontalAccuracy: 如果整个数字是负数, 就代表位置数据无效
  verticalAccuracy:  如果整个数字是负数, 就代表海拔数据无效
  course: 航向
  speed: 速度
  distanceFromLocation: 计算两个经纬度坐标之间的物理指向距离

使用之前, 务必判断数据是否有效

功能: 如果水平精确度小于零, 代表虽然可以获取位置对象, 但是数据错误, 不可用
代码: if (location.horizontalAccuracy < 0) return;

案例

记录行走的方向距离

print("获得位置")
        guard let newLocation = locations.last else
        {
            return;
        }
        if newLocation.horizontalAccuracy < 0
        {
            return;
        }
       // 场景演示:打印当前用户的行走方向,偏离角度以及对应的行走距离,
       //  例如:”北偏东 30度 方向,移动了 8米”
        //1. 获得方向
        let angleStrs = ["北偏东", "东偏南", "南偏西", "西偏北"]
        let index = Int(newLocation.course) / 90
        var angleText = ""
        
        angleText = "向\(angleStrs[index])方向行走"

        //2.行走偏离的角度
        let angle = newLocation.course.truncatingRemainder(dividingBy: 90)
        
        if Int(angle) == 0
        {
            let courseStr = angleStrs[index]
            let direction = (courseStr as NSString).substring(to: 1)
            angleText = "向正\(direction)方向行走"
            
        }
        angleLabel.text = angleText
        print("航向\(newLocation.course)")
        print("速度\(newLocation.speed)")
        print(angleStrs[index])
        
        //3.行走了多少米
        let lastL = lastLocation ?? newLocation
        let distance = newLocation.distance(from: lastL)
        lastLocation = newLocation
        
        newDistanceLabel.text = "行走了\(distance)米"

相关文章

网友评论

      本文标题:CLLocation对象

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