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:
- Rotate
MKAnnotationView
'simage
property:- Method: When the heading changes, create a rotated copy of the
UIImage
and assign it to theimage
property. Example (not tested). - Con: Can't easily animate the rotation.
- Method: When the heading changes, create a rotated copy of the
- Rotate the
MKAnnotationView
itself:- Method: When the heading changes, use
MKAnnotationView
's/UIView
'stransform
property. Assign an appropriateCGAffineTransform
to it. - Pro: Easiest
- Con: Also rotates the detail/callout views. If you need these, this will not be an option for you.
- Method: When the heading changes, use
- Put the
UIImage
into aUIImageView
and add that one as a subview toMKAnnotationView
:- Method: Similar to 2. but use the
transform
property on theUIImageView
directly not on theMKAnnotationView
itself. This way callout views are not rotated. - Pro: Should work well.
- Con: A bit more work.
- Method: Similar to 2. but use the
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 inUIView
's staticanimate
method.
Related Topics
How to Specify Japanese Encoding for a UIlabel
Swift Lose Precision in Decimal Formatting
Lsapplicationqueriesschemes and Derived Data
Error: 'string' Is Not Convertible to 'string!'
Swift Making Copies of Passed Class Instances
How to Write to a Variable from Within The Firebase Getdocument Function (Swift)
Converting Scanlocation from Utf16 Units to Character Index in Nsscanner (Swift)
Vapor 3 - How to Check for Similar Email Before Saving Object
How to Mirror The Design of The Codable/Codablekeys Protocols
Finding The First Non-Repeating Character in a String Using Swift
How to Use The Snapchat Sdk (Snapkit) with Swiftui
How to Keep a User Logged In? Swift
Bridgetoobjectivec Not Available on Swift Beta 5
Location Access Request in iOS 11
How to Use Swipe to Dismiss While Presenting a Fullscreen Modal in iOS 13