3

I've got two custom MKAnnotationViews, which display fine when they are initially added to the MKMapView.

The issue is, when rotating, or zooming out, their drawn locations seem to become more and more off. To reiterate, the closer I zoom in, the more accurate they appear, but when zooming out and rotating they are completely off. Any ideas?

Looks good (initial state):

First image

Looks bad (and wrong):

Second image

My viewForAnnotation method is pretty basic, nothing fancy going on here.

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
    }

Update

I suspected this was somewhat related to anchor points, and I was able to fix the "parking pin annotation views" rotation by view.layer.anchorPoint = CGPoint(x: 0.5, y: 1.0), but had no luck with driver annotation view (the one w/ the car)

Appreciate the help SO!

royherma
  • 4,095
  • 1
  • 31
  • 42

1 Answers1

2

You should set the centerOffset of your MKAnnotationView. As the documentation says:

By default, the center point of an annotation view is placed at the coordinate point of the associated annotation. You can use this property to reposition the annotation view as needed. This x and y offset values are measured in points. Positive offset values move the annotation view down and to the right, while negative values move it up and to the left.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • For the driver annotation view, I believe the coordinate point should be the center of the view, since its place within a square and spans the whole view, thus how would changing the center offset help? For the pin annotation view, I was able to resolve with the anchor point adjustment mentioned in the update. – royherma Jul 04 '17 at 18:40
  • 1
    I would not change anchor points. This is the purpose of `centerOffset`. – Rob Jul 04 '17 at 18:44
  • Ok, so lets say my driver annotation view is 100 x 100 points, you would make the centerOffset 0,50? – royherma Jul 04 '17 at 19:06
  • 0, -50 to shift it up, I believe. – Rob Jul 04 '17 at 19:08
  • the cars front is at the top so down would seem more sensible? – royherma Jul 04 '17 at 19:10
  • Sure. I was focusing on the pin. Yep, if you want to move the car down, you’d use positive number. – Rob Jul 04 '17 at 19:12
  • as suspected, this didn't fix the issue, merely just offset the frame of the view by y. – royherma Jul 05 '17 at 10:20
  • Yeah, the `centerPoint` change would affect the location of the annotation view with respect to the annotation’s `coordinate`, if the annotation view wasn't quite centered appropriately. But if you’re seeing radical change of `coordinate`, then there’s something else going on, too. I’d suspect something going on with the annotation itself (e.g. are you instantisting a new annotation object each time? or are you referencing some property that may get be mutating on us?). We may need to see some annotation code, as I don’t see anything obviously wrong in the `viewFor` code. – Rob Jul 05 '17 at 16:26
  • By the way, I might suggest commenting out the whole `viewFor` method and rely upon standard annotation views. That will confirm whether the behavior is this method or something going on with the annotations themselves. – Rob Jul 05 '17 at 16:46