Swift Mkannotationview Rotate

Swift MKAnnotationView Rotate

It works for me in this way (Yes, I used some code part from your example :)
The main thing is to get an annotation view and change (rotate) the image then:

for tempAnnotation in self.mapView.annotations as! [RouteAnnotation]
{
tempAnnotation.angle = annotations[self.annotationIDs[tempAnnotation.routeId]!].angle
let annotationView: MKAnnotationView = self.mapView.viewForAnnotation(tempAnnotation)!
var annotationViewImage = annotationView.image
annotationViewImage = imageResize(image: UIImage(named:"arrowPin.png")!,sizeChange: CGSizeMake(48, 48)).imageRotatedByangles(CGFloat(tempAnnotation.angle), flip: false)
annotationView.image = annotationViewImage
}

// Route annotation class

class RouteAnnotation : MKPointAnnotation {
var angle: Int = 0
var routeId: Int = 0
}

//UIImage extension

extension UIImage {
public func imageRotatedByangles(angles: CGFloat, flip: Bool) -> UIImage {
let anglesToRadians: (CGFloat) -> CGFloat = {
return $0 / 180.0 * CGFloat(M_PI)
}

// calculate the size of the rotated view's containing box for our drawing space
let rotatedViewBox = UIView(frame: CGRect(origin: CGPointZero, size: size))
let t = CGAffineTransformMakeRotation(anglesToRadians(angles));
rotatedViewBox.transform = t
let rotatedSize = rotatedViewBox.frame.size

// Create the bitmap context
UIGraphicsBeginImageContext(rotatedSize)
let bitmap = UIGraphicsGetCurrentContext()

// Move the origin to the middle of the image so we will rotate and scale around the center.
CGContextTranslateCTM(bitmap, rotatedSize.width / 2.0, rotatedSize.height / 2.0);

// // Rotate the image context
CGContextRotateCTM(bitmap, anglesToRadians(angles));

// Now, draw the rotated/scaled image into the context
var yFlip: CGFloat

if(flip){
yFlip = CGFloat(-1.0)
} else {
yFlip = CGFloat(1.0)
}

CGContextScaleCTM(bitmap, yFlip, -1.0)
CGContextDrawImage(bitmap, CGRectMake(-size.width / 2, -size.height / 2, size.width, size.height), CGImage)

let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

return newImage
}
}

Hope, it will help somebody, who have the same problems

Rotate MKAnnotation image not title

I have figured it out! I just needed to rotate only the image instead of rotating the whole view.

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
if !(annotation is CustomPointAnnotation) {
return nil
}

let reuseId = "annotation"

var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
if anView == nil {
anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
anView!.canShowCallout = true
}
else {
anView!.annotation = annotation
}

let cpa = annotation as! CustomPointAnnotation
var imagePin = UIImage(named:cpa.imageName)
imagePin = imagePin?.rotateImage(cpa.courseDegrees)
anView!.image = imagePin

return anView
}

How to rotate custom userLocationAnnotationView image using MapKit in swift?

I can't give you complete code examples at this point but I hope I can give you some pointers to go off on.

First, I don't think you would necessarily have to subclass MKAnnotationView. You could simply assign your UIImage to its image property. I think that would make things easier unless you do require the customization.

Now, I assume you have successfully added the annotation to the map and have a reference to it.

To rotate the heading indicator, I see three options:

  1. Rotate MKAnnotationView's image property:

    • Method: When the heading changes, create a rotated copy of the UIImage and assign it to the image property. Example (not tested).
    • Con: Can't easily animate the rotation.
  2. Rotate the MKAnnotationView itself:

    • Method: When the heading changes, use MKAnnotationView's/UIView's transform property. Assign an appropriate CGAffineTransform to it.
    • Pro: Easiest
    • Con: Also rotates the detail/callout views. If you need these, this will not be an option for you.
  3. Put the UIImage into a UIImageView and add that one as a subview to MKAnnotationView:

    • Method: Similar to 2. but use the transform property on the UIImageView directly not on the MKAnnotationView itself. This way callout views are not rotated.
    • Pro: Should work well.
    • Con: A bit more work.

What you also need:

  • A function to convert from degrees to radians. The affine transformations require radians.
  • If you want to animate the rotation (except for 1.) wrap the change to the transform property in UIView's static animate method.


Related Topics



Leave a reply



Submit