更改 MKMarkerAnnotationView 大小

tuy*_* le 1 mapkit mkmapview mkmapviewdelegate swift

如何改变MKMarkerAnnotationView尺寸?

我尝试设置annotationView.bounds.size = CGSize(width: 50, height: 50),但看起来大小没有改变。我还尝试打印视图的大小,看起来默认为 28,28

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
      guard annotation is MKPointAnnotation else { return nil }


      let annotationView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: Constant.Indentifier.mapPoint)
      annotationView.canShowCallout = true
      annotationView.animatesWhenAdded = true
      annotationView.glyphImage = UIImage(systemName: "house.fill")
      annotationView.glyphTintColor = .systemBlue
      annotationView.markerTintColor = .white
      print(annotationView.bounds.size) // defaulted to 28,28
      annotationView.bounds.size = CGSize(width: 50, height: 50) // Does not change bubble size
      return annotationView
}
Run Code Online (Sandbox Code Playgroud)

Rob*_*Rob 8

请参阅glyphImage 文档,其中讨论了字形的大小:

\n\n
\n

当标记处于正常状态时显示字形图像。创建字形图像作为模板图像,以便可以对其应用字形色调颜色。通常,您将此图像的大小在 iOS 上设置为 20 x 20 点,在 tvOS 上设置为 40 x 40 点。但是,如果您没有在selectedGlyphImage,请在 iOS 上将此图像的大小设置为 40 x 40 点,在 tvOS 上将其大小设置为 60 x 40 点。MapKit 会缩放大于或小于这些尺寸的图像。

\n
\n\n

底线是,MKMarkerAnnotationView它的两种状态(选定和未选定)具有固定的大小。

\n\n

如果你想要更大的注释视图,你\xe2\x80\x99会想要编写你自己的MKAnnotationView. 例如,简单地创建一个大房子图像相对容易:

\n\n
class HouseAnnotationView: MKAnnotationView {\n    override init(annotation: MKAnnotation?, reuseIdentifier: String?) {\n        super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)\n\n        let configuration = UIImage.SymbolConfiguration(pointSize: 50)\n        image = UIImage(systemName: "house.fill", withConfiguration: configuration)\n    }\n\n    required init?(coder aDecoder: NSCoder) {\n        fatalError("init(coder:) has not been implemented")\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

顺便说一下,我\xe2\x80\x99d建议注册这个注释视图类,如下所示,然后删除mapView(_:viewFor:)完全删除该方法。

\n\n
mapView.register(HouseAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)\n
Run Code Online (Sandbox Code Playgroud)\n\n

现在,上面的注释视图仅渲染一个大的 \xe2\x80\x9chouse\xe2\x80\x9d 图像。如果你想要它在泡沫中,就像MKMarkerAnnotationView现在这样,你\xe2\x80\x99就必须自己画它:

\n\n
class HouseAnnotationView: MKAnnotationView {\n    override init(annotation: MKAnnotation?, reuseIdentifier: String?) {\n        super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)\n\n        configureImage()\n        configureView()\n        configureAnnotationView()\n    }\n\n    required init?(coder aDecoder: NSCoder) {\n        fatalError("init(coder:) has not been implemented")\n    }\n}\n\nprivate extension HouseAnnotationView {\n    func configureImage() {\n        let radius: CGFloat = 25\n        let center = CGPoint(x: radius, y: radius)\n        let rect = CGRect(origin: .zero, size: CGSize(width: 50, height: 60))\n        let angle: CGFloat = .pi / 16\n\n        let image = UIGraphicsImageRenderer(bounds: rect).image { _ in\n            UIColor.white.setFill()\n            let path = UIBezierPath(arcCenter: center, radius: radius, startAngle: .pi / 2 - angle, endAngle: .pi / 2 + angle, clockwise: false)\n            path.addLine(to: CGPoint(x: rect.midX, y: rect.maxY))\n            path.close()\n            path.fill()\n\n            let configuration = UIImage.SymbolConfiguration(pointSize: 24)\n            let house = UIImage(systemName: "house.fill", withConfiguration: configuration)!\n                .withTintColor(.blue)\n            house.draw(at: CGPoint(x: center.x - house.size.width / 2, y: center.y - house.size.height / 2))\n        }\n\n        self.image = image\n        centerOffset = CGPoint(x: 0, y: -image.size.height / 2) // i.e. bottom center of image is where the point is\n    }\n\n    func configureView() {\n        layer.shadowColor = UIColor.black.cgColor\n        layer.shadowRadius = 5\n        layer.shadowOffset = CGSize(width: 3, height: 3)\n        layer.shadowOpacity = 0.5\n    }\n\n    func configureAnnotationView() {\n        canShowCallout = true\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

得出:

\n\n

在此输入图像描述

\n\n

但即便如此,\xe2\x80\x99 也无法重现所有行为MKMarkerAnnotationView。所以这一切都取决于有多少MKMarkerAnnotationView您需要多少行为/外观,以及拥有更大的注释视图是否值得所有这些努力。

\n