仿悦跑圈轨迹回放

运动轨迹回放高德官方有demo,但需要运动物实时居中

代码在这里

先上最后效果图

创建CADisplayLink当作计时器,不要用NSTimer(不精确)

frameInterval 帧间隔,相当于每隔多久刷新一次 (设置为1,代表1/60秒刷新一次,实例中设的是2帧)

  dpLink = CADisplayLink(target: self, selector: #selector(ViewController.update))
  dpLink?.frameInterval = minframeInterval
  dpLink?.isPaused = false
  dpLink?.add(to: RunLoop.current, forMode: RunLoopMode.commonModes)

复制代码

在update方法中持续改变地图中心,地图旋转角度,地图相机角度

  self.mapView.setCenter(traceCoordinates[uptateIndex+1], animated: false)
  self.mapView.setRotationDegree(CGFloat(yvAngle) , animated: false, duration: 1)
  self.mapView.setCameraDegree( CGFloat(yvAngle), animated: false, duration: 1)

复制代码

uptateIndex 每次加1,temporarytraceCoordinates临时存当前位置之前的所有值,polyline每次替换为后一条

 if  let line = self.polyline  {
     self.mapView.remove(line)
 }
 temporarytraceCoordinates.append(traceCoordinates[uptateIndex])
 polyline = MAPolyline(coordinates: &temporarytraceCoordinates, count: UInt(temporarytraceCoordinates.count))

复制代码

代理方法里改变线条颜色

func mapView(_ mapView: MAMapView!, rendererFor overlay: MAOverlay!) -> MAOverlayRenderer! {
        if overlay.isKind(of: MAPolyline.self) {
            let renderer: MAPolylineRenderer = MAPolylineRenderer.init(polyline: overlay as! MAPolyline!)
            renderer.lineWidth = 8.0
            renderer.strokeColor = UIColor(red: 0, green: 230, blue: 239, alpha: 1)
            return renderer
        }
        return nil
 }
复制代码

poiAnnotationView?.image 设置运动物体的图片

    func mapView(_ mapView: MAMapView, viewFor annotation: MAAnnotation) -> MAAnnotationView? {
        
        if annotation.isEqual(myLocation) {
            let annotationIdentifier = "myLcoationIdentifier"
            var poiAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier)
            if poiAnnotationView == nil {
                poiAnnotationView = MAAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
            }
            poiAnnotationView?.image = UIImage(named: "userHeadimage")
            poiAnnotationView?.imageView.layer.cornerRadius = 20
            poiAnnotationView?.imageView.layer.masksToBounds = true
            poiAnnotationView?.imageView.backgroundColor = UIColor.white
            poiAnnotationView?.imageView.layer.borderColor = UIColor.white.cgColor
            poiAnnotationView?.imageView.layer.borderWidth = 2
            poiAnnotationView!.canShowCallout = false
            return poiAnnotationView
        }
        return nil
    }

复制代码

发现bug或好的建议欢迎 issues or Email Yvente@163.com

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值