Question

I have trouble with MKAnnotationView Frame. Before asking I want to clarify some things.

First:

I have a MKMapView and some custom annotation: enter image description here

It looks ok.

Second I draw a Line via MKPolylineView between user's location and this custom annotation:

enter image description here

Looks pretty good

Third, Then I tried to change default annotation image by my own custom:

enter image description here

Right here we have wrong result, coz annotation not not connected with line

Forth, Okey I tried to change position of this image:

enter image description here

I just added to MKAnnotationView a new UIView with my UIImageView.

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{

    MKAnnotationView *annotationView = nil;
    annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"pin"];

    if(!annotationView)
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:person reuseIdentifier:@"pin"];
    else
        annotationView.annotation = annotation;



    UIImageView *avatarImage = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 25, 25)];
    avatarImage.image = [UIImage imageNamed:avatarName];

    [annotationView setImage:nil];

    avatarImage.layer.masksToBounds = YES;

    UIView *avatarView = [[UIView alloc] initWithFrame:CGRectMake(-7, 23, 30, 30)];
    avatarImage.backgroundColor = [UIColor clearColor];
    avatarView.layer.cornerRadius = 15.0f;


     [avatarView addSubview:avatarImage];
     [annotationView addSubview:avatarView];

     return annotationView;
}

After all:

enter image description here

This is What I want :)

But sometimes when I tap this annotation didSelectAnnotationView: isn't called. I know why, coz my UIView with my image out of MKAnnotation.

I changed background color of MKAnnotation:

enter image description here

When I tap this red region didSelectAnnotationView: is called.

SO, question is how can I change frame of this MKAnnotationView so that my UIView will not be outside?

P.S. I tried change frame, doesn't work :D

P.S.S Sorry for my English :(

Was it helpful?

Solution 2

I've solved this problem. I just replaced MKPinAnnotationView with MKAnnotationView, and I get what I need.

 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
                annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"pin"];

                if(!annotationView)
                    annotationView = [[MKAnnotationView alloc] initWithAnnotation:person reuseIdentifier:@"pin"];
                else
                    annotationView.annotation = annotation;

                /*
                 Avatar Image
                 */
                annotationView.canShowCallout = NO;

                [annotationView setImage:[self image:[UIImage imageNamed:avatarName] scaledToSize:CGSizeMake(25, 25)]]; 
}

OTHER TIPS

I have a long explanation on custom callout view here https://stackoverflow.com/a/19404994/1226370.

All details about touch handling are described in linked answer.

Hope it will be useful!

Update 1

After you will subclass your MKAnnotationView or MKPinAnnotationView you will implement two methods as follows:

- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event
{
    UIView* hitView = [super hitTest:point withEvent:event];
    if (hitView != nil)
    {
        [self.superview bringSubviewToFront:self];
    }
    return hitView;
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event
{
    CGRect rect = self.bounds;
    BOOL isInside = CGRectContainsPoint(rect, point);
    if(!isInside)
    {
        for (UIView *view in self.subviews)
        {
            isInside = CGRectContainsPoint(view.frame, point);
            if(isInside)
                break;
        }
    }
    return isInside;
}

In short, in those methods you specify whether touch at current location should be treated as touch on MKAnnotationView or not.

So if you want to increase touch area of annotation you would have to make proper changes to those methods.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top