【发布时间】:2016-09-15 10:03:45
【问题描述】:
我正在尝试找到一种使用 MapKit 在地图上显示用户方向的方法。 这样做的本机 MapKit 方式总是旋转整个地图。 由于用户位置也是一个 MKAnnotationView,所以我决定创建一个特定的类来覆盖它并使用特定的图像(带有箭头)。
class UserLocationAnnotationView: MKAnnotationView {
override init(frame: CGRect) {
super.init(frame: frame)
}
override init(annotation: MKAnnotation!, reuseIdentifier: String!) {
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
var frame = self.frame
frame.size = CGSizeMake(130, 130)
self.frame = frame
self.backgroundColor = UIColor.clearColor()
self.centerOffset = CGPointMake(-30, -50)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
*/
override func drawRect(rect: CGRect) {
// Drawing code
UIImage(named: "userLocation.png")?.drawInRect(CGRectMake(65, 65, 65, 65))
}
现在我正在尝试找到一种方法来在 locationManager 的 didUpdateHeading 函数中旋转 MKAnnotationView 图像。
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
var userLocationView :MKAnnotationView?
func locationManager(manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
print(newHeading.magneticHeading)
}
newHeading.magneticHeading 的打印效果很好,而且非常准确。 现在如何旋转我的自定义 UserLocationAnnotationView ?
感谢您的帮助。
【问题讨论】:
标签: ios swift mapkit mkannotationview direction