How to resize MKAnnotationView on iOS6?
Asked Answered
B

1

3

Resize MKAnnotationView Image When map zooms in and out? This methord are successful on iOS5, but failed on iOS6.

I change the MKAnnotationView's transform directly, and no luck. The MKAnnotationView only resize in a flash(When touch up inside the MKMapView, after the touch up finish, the MKAnnotationView will restore the original size).

My code are below:

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
for (id <MKAnnotation>annotation in _mapView.annotations) {
    // if it's the user location, just return nil.
    if ([annotation isKindOfClass:[MKUserLocation class]])
        continue;

    // handle our custom annotations
    //
    if ([annotation isKindOfClass:[XPMKAnnotation class]])
    {
        // try to retrieve an existing pin view first
        MKAnnotationView *pinView = [_mapView viewForAnnotation:annotation];
        //resize the pin view
        double zoomLevel = [_mapView getZoomLevel];
        double scale = (1.0 * zoomLevel / 16) + 0.5;
        pinView.transform = CGAffineTransformMakeScale(scale, scale);
    }
    }
}

Can we resize the MKAnnotationView on iOS6? Anybody know any way?

Bedad answered 28/9, 2012 at 17:52 Comment(1)
Having that same issue here. Mine looks like it's flickering, as if the transform keeps getting reset for the AnnotationView. Will post here if I find a solution.Maus
M
3

Apple suggests resizing the .image property of an annotation view. In my case shown below, I looked at the UIImage that was set in the viewforAnnotation and re-scaled it to the zoom level in a UIPinchGestureRecognizer

   UIImage *orangeImage = [UIImage imageNamed:@"Orange210.PNG"];
    CGRect resizeRect;
    //rescale image based on zoom level
    resizeRect.size.height = orangeImage.size.height * scale;
    resizeRect.size.width = orangeImage.size.width  * scale ;
    NSLog(@"height =  %f, width = %f, zoomLevel = %f", resizeRect.size.height, resizeRect.size.width,zoomLevel );
    resizeRect.origin = (CGPoint){0,0};
    UIGraphicsBeginImageContext(resizeRect.size);
    [orangeImage drawInRect:resizeRect];
    UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    pinView.image = resizedImage;
Macroscopic answered 24/10, 2012 at 1:53 Comment(5)
Thank Erik Gross, very help for me.Bedad
FYI, getZoom is from a category discussed here: #7657707Adventuress
what is the Scale hereEdh
Vishnu and @Quincy. You can find the definition of scale from the question pointed in first line of this question. See the answerSatanic
I have implemented the same but my image is not resizing.Satanic

© 2022 - 2024 — McMap. All rights reserved.