【发布时间】:2021-11-19 02:28:57
【问题描述】:
我正在构建一个依赖 MapKit 的 SwiftUI 应用程序。
但我在渲染自定义图钉标记时遇到了问题。
每当我添加图钉时,它都会从图像的中心渲染,因此它不会与当前位置正确对齐。
我尝试过添加偏移量,更改原点,但自定义图钉图像基本上超出了范围。
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let view = MKAnnotationView(annotation: annotation, reuseIdentifier: nil)
let size = CGSize(width: 35, height: 40)
UIGraphicsBeginImageContext(size)
UIImage(named: "customPin")?.draw(in: CGRect(origin: CGPoint(x: 0, y: 20), size: size))
//let context = UIGraphicsGetCurrentContext()!
//context.move(to: CGPoint(x:0, y: -200))
// This code didn't affect the rendering
view.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
view.canShowCallout = true
return view
}
基本上我认为正在发生的事情是我正在应用的偏移量正在移动图像,但 CGRect 的边界保持在同一个位置。我不知道如何抵消两者。
【问题讨论】:
标签: swift swiftui mapkit mapkitannotation