Size image pin annotation
Asked Answered
S

2

17

I put the personal image instead of the traditional red pin. When I open the map to display the pin, the image cover the entire map. Is there a maximum size of the pin image or how do I integrate something in the code to fit the size standard classic pin?

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation {
        return nil
    }

    let annotationIdentifier = "SomeCustomIdentifier" // use something unique that functionally identifies the type of pin

    var annotationView: MKAnnotationView! = mapView.dequeueReusableAnnotationViewWithIdentifier(annotationIdentifier)

    if annotationView != nil {
        annotationView.annotation = annotation
    } else {
        annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)

        annotationView.image = UIImage(named: "pin maps.png")

        annotationView.canShowCallout = true
        annotationView.calloutOffset = CGPointMake(-8, 0)

        annotationView.autoresizesSubviews = true
        annotationView.rightCalloutAccessoryView = UIButton(type: UIButtonType.DetailDisclosure) as UIView
    }

    return annotationView
}
Stoa answered 21/10, 2015 at 15:1 Comment(0)
C
34

There is not a maximum size of the pin image. You need to resize UIImage.

    let annotationIdentifier = "SomeCustomIdentifier"
    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier)
    if annotationView == nil {
        annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
        annotationView?.canShowCallout = true

        // Resize image
        let pinImage = UIImage(named: "pin maps.png")
        let size = CGSize(width: 50, height: 50)
        UIGraphicsBeginImageContext(size)
        pinImage!.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
        let resizedImage = UIGraphicsGetImageFromCurrentImageContext()

        annotationView?.image = resizedImage

        let rightButton: AnyObject! = UIButton(type: UIButtonType.detailDisclosure)
        annotationView?.rightCalloutAccessoryView = rightButton as? UIView
    }
    else {
        annotationView?.annotation = annotation
    }
Contraindicate answered 22/10, 2015 at 1:34 Comment(3)
it says CGRectMake is not in swftPopulace
pinImage.draw(in: CGRect(x:0, y:0, width:size.width, height:size.height))Broach
I would use: anView?.frame.size = CGSize(width: 30, height: 40) Instead of all the drawContext stuffJudges
A
10

I know there is an accepted answer already, but it did not work for me. Kosuke Ogawa is correct that there is no maximum size and you must instead do some resizing. However, I found that modifying the Frame on the MKAnnotationView produces better results.

Kiko Lobo commented the solution that worked best for me, so all credit to him.

Instead of doing anything with the UIImage, you only need to edit the MKAnnotationView. Kibo Lobo's comment:

annotationView?.frame.size = CGSize(width: 30, height: 40)

I actually did this in C# with Xamarin, that looks like this:

annotationView.Frame = new CGRect(0,0,30,40);

The accepted answer had no effect when implemented in Xamarin. Hope this helps anyone else encountering image scaling issues. The UIImage.Scale() method made the image very fuzzy, while modifying the Frame kept the quality the same.

Aglimmer answered 7/1, 2021 at 8:42 Comment(1)
This is a great answer and it worked very good on the latest iOS15 as well! Used in a SwiftUI UIViewRepresentableEluvium

© 2022 - 2024 — McMap. All rights reserved.