【问题标题】:Custom MKAnnotationView location drastically changes when rotate or zoom in/out of map旋转或放大/缩小地图时,自定义 MKAnnotationView 位置会发生巨大变化
【发布时间】:2017-07-04 18:23:59
【问题描述】:

我有两个自定义MKAnnotationViews,当它们最初添加到MKMapView 时显示正常。

问题是,当旋转或缩小时,它们绘制的位置似乎变得越来越偏离。重申一下,我放大得越近,它们出现的越准确,但是当缩小和旋转时,它们会完全关闭。有什么想法吗?

看起来不错(初始状态):

看起来很糟糕(而且是错误的):

我的viewForAnnotation 方法非常基本,这里没什么特别的。

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


        if let annotation = annotation as? PKDriverAnnotation
            {
            let identifier = "driver"
            var annotationView: PKDriverAnnotationView
            if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) { // 2
                dequeuedView.annotation = annotation
                annotationView = dequeuedView as! PKDriverAnnotationView
            } else {
                // 3
                annotationView = PKDriverAnnotationView(annotation: annotation, reuseIdentifier: identifier)
                annotationView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(PKTransactionMapViewController.annotationViewTapped(recognizer:))))
            }

            self.driverAnnotationView = annotationView

            return annotationView
        } else if let annotation = annotation as? PKAnnotation {
            let identifier = "pin"
            var view: MKAnnotationView
            if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) { // 2
                dequeuedView.annotation = annotation
                view = dequeuedView
            } else {
                // 3
                view = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)
                view.image = UIImage(named: "TransactionAnnotation")
                view.canShowCallout = false
                view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(PKHomeViewController.annotationViewTapped(recognizer:))))

                let profilePic = FBSDKProfilePictureView(frame: CGRect(x: 4, y: 4, width: 43, height: 43))
                profilePic.center = CGPoint(x: view.bounds.midX, y: profilePic.center.y)
                profilePic.profileID = self.transaction!.availableParking.holder.fbid
                profilePic.layer.cornerRadius = 21.0
                profilePic.clipsToBounds = true
                view.addSubview(profilePic)
            }

            return view
        }
        return nil
    }

更新

我怀疑这与锚点有点相关,我能够通过view.layer.anchorPoint = CGPoint(x: 0.5, y: 1.0) 修复“停车销注释视图”旋转,但驾驶员注释视图(与汽车的视图)没有运气

感谢您的帮助!

【问题讨论】:

    标签: ios objective-c swift mkmapview mkannotationview


    【解决方案1】:

    您应该设置MKAnnotationViewcenterOffset。正如文档所说:

    默认情况下,注释视图的中心点位于关联注释的坐标点。您可以根据需要使用此属性重新定位注释视图。此 x 和 y 偏移值以点为单位进行测量。正偏移值将注释视图向下和向右移动,而负值将它向上和向左移动。

    【讨论】:

    • 对于驱动注释视图,我认为坐标点应该是视图的中心,因为它位于正方形内并跨越整个视图,因此更改中心偏移量有什么帮助?对于引脚注释视图,我能够通过更新中提到的锚点调整来解决。
    • 我不会更改锚点。这就是centerOffset的目的。
    • 好的,假设我的驱动注释视图是 100 x 100 点,你会让 centerOffset 为 0,50?
    • 0, -50 向上移动,我相信。
    • 车头在顶部,所以向下看起来更明智?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多