運動軌跡回放高德官方有demo葡缰,但需要運動物實時居中
代碼在這里
先上最后效果圖
2018-01-18 16_55_29.gif
創(chuàng)建CADisplayLink當作計時器剩蟀,不要用NSTimer(不精確)
frameInterval 幀間隔,相當于每隔多久刷新一次 (設(shè)置為1映皆,代表1/60秒刷新一次链快,實例中設(shè)的是2幀)
dpLink = CADisplayLink(target: self, selector: #selector(ViewController.update))
dpLink?.frameInterval = minframeInterval
dpLink?.isPaused = false
dpLink?.add(to: RunLoop.current, forMode: RunLoopMode.commonModes)
在update方法中持續(xù)改變地圖中心凡辱,地圖旋轉(zhuǎn)角度,地圖相機角度
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 設(shè)置運動物體的圖片
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
}
發(fā)現(xiàn)bug或好的建議歡迎 issues or Email Yvente@163.com