首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在MKMapView上禁止旋转但显示标题?

如何在MKMapView上禁止旋转但显示标题?
EN

Stack Overflow用户
提问于 2014-08-13 23:08:11
回答 1查看 905关注 0票数 4

对于我的项目,我想把用户的头像放在MKMapView上,而不旋转地图(用蓝色的圆锥体)。

Here's a gist

此外,在启用mapView.setUserTrackingMode(MKUserTrackingMode.FollowWithHeading, animated: true)的情况下,我无法在地图中导航。

我尝试在mapView:didChangeUserTrackingMode上设置trackingMode,但它不工作。

有什么想法吗?

EN

回答 1

Stack Overflow用户

发布于 2017-06-22 17:21:07

我也有同样的问题,并通过使用CLLocationManagerlocationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading)方法解决了它:

代码语言:javascript
复制
class MyViewController: UIViewController {

    @IBOutlet weak var mapView: MKMapView!
    weak var userAnnotationView: MKAnnotationView?
    var locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()
        mapView.showsUserLocation = true
        mapView.delegate = self
        locationManager.delegate = self
        locationManager.startUpdatingHeading()
    }
}

// MARK: - MKMapViewDelegate
extension MyViewController: MKMapViewDelegate {
    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        if let annotation = annotation as? MKUserLocation {
            // User
            let reuseIdentifier = "UserAnnotationView"
            let annotationView: MKAnnotationView
            if let view = mapView.dequeueReusableAnnotationView(withIdentifier: reuseIdentifier) {
                annotationView = view
            } else {
                annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
            }
            annotationView.image = UIImage(named: "userLocationWithoutHeading")
            userAnnotationView = annotationView
            return annotationView
        } else {
            return nil
        }
    }
}

// MARK: - CLLocationManagerDelegate
extension MyViewController: CLLocationManagerDelegate {
    func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
        guard let userAnnotationView = userAnnotationView else { return }
        userAnnotationView.image = UIImage(named: "arrowUp")
        let rotationAngle = heading.magneticHeading * Double.pi / 180.0
        userAnnotationView.transform = CGAffineTransform(rotationAngle: CGFloat(rotationAngle))
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25289775

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档