首页 > 解决方案 > 如何通过标题快速旋转标记?

问题描述

在此处输入图像描述

嗨,伙计们,我正在尝试通过从套接字接收标题来更新此注释的位置,因此每次我更新标题时,图像都会返回到其原始状态,然后在更新后图像也会返回到从套接字接收的新标题,如下所示它从从套接字接收的标题向左旋转(小偏差)这是我用来更新标题的代码:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {


        guard !(annotation is MKUserLocation) else {
            return nil
        }
        let annotationIdentifier = "marker"
        var annotationView: MKAnnotationView?
         let unitAnnotation = annotation as? Unit
        if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) {
            annotationView = dequeuedAnnotationView
            annotationView?.annotation = annotation
        }else {
            annotationView = unitAnnotation?.annotationView()

            annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
        }


        if let annotationView = annotationView {

            annotationView.canShowCallout = true
            let image = #imageLiteral(resourceName: "Arrow32")
                let imageHeight  = image.size.height
            annotationView.image = image
            annotationView.tintColor = UIColor.blue

         annotationView.centerOffset = CGPoint(x: 0, y: -imageHeight / 2)
            self.addBounceAnimationToView(view: annotationView)
        }

        UIView.animate(withDuration: 0.5) {
            if let heading = unitAnnotation?.heading{
                //annotationView?.annotation?.= unitAnnotation?.coordinate

            annotationView?.transform = self.mapKit.transform.rotated(by: CGFloat(self.degreesToRadians(degrees: heading+20)))
            }
        }
        return annotationView

    } 

任何帮助将不胜感激,非常感谢。

标签: swiftmapkitreal-timemapkitannotation

解决方案


locationManager.startUpdatingHeading()  



func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
            UIView.animate(withDuration: 0.5) {
                let angle = CGFloat(newHeading.trueHeading).toRadians
                self.markerView.transform = CGAffineTransform(rotationAngle: angle)
            }
        }

转换为弧度的扩展

extension CGFloat {
        var toRadians: CGFloat { return self * .pi / 180 }
        var toDegrees: CGFloat { return self * 180 / .pi }
    }

推荐阅读