How to change MKMarkerAnnotationView size?
I tried to set annotationView.bounds.size = CGSize(width: 50, height: 50) but it does not look like the size has changed. I also tried to print out the size of the view and looks like it is defaulted to 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
}
See
glyphImagedocumentation, which talks about the size of the glyph:Bottom line, the
MKMarkerAnnotationViewhas a fixed sizes for its two states, selected and unselected.If you want a bigger annotation view, you’ll want to write your own
MKAnnotationView. E.g., simply creating a large house image is relatively easy:By the way, I’d suggest registering this annotation view class, like below, and then removing the
mapView(_:viewFor:)method entirely.Now, the above annotation view only renders a large “house” image. If you want it in a bubble, like
MKMarkerAnnotationViewdoes, you’ll have to draw that yourself:That yields:
But even that doesn’t reproduce all of the
MKMarkerAnnotationViewbehaviors. So it all comes down to how much of theMKMarkerAnnotationViewbehaviors/appearance you need and whether having a larger annotation view is worth all of that effort.