14

I've been searching for a similar solution on how to design a custom MKAnnotationView like this one.

enter image description here

I'v subclassed the MKAnnotation and i was able to add an image named F.png the F.png is a frame image as showed in the picture. what i want is to add an inner image. (colored Blue in the picture i draw)

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
{    
if ([annotation isKindOfClass:[MKUserLocation class]])
    return nil;
static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
if(annotationView)
    return annotationView;
else
{
    MKAnnotationView *annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
    annotationView.canShowCallout = YES;
    annotationView.image = [UIImage imageNamed:[NSString stringWithFormat:@"F.png"]];        
    UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [rightButton addTarget:self action:@selector(writeSomething:) forControlEvents:UIControlEventTouchUpInside];
    [rightButton setTitle:annotation.title forState:UIControlStateNormal];

    annotationView.rightCalloutAccessoryView = rightButton;
    annotationView.canShowCallout = YES;
    annotationView.draggable = NO;
    return annotationView;
}
return nil;
}
Kassem
  • 1,481
  • 3
  • 20
  • 42
  • the only thing i was able to do is to change the image of the annotation, but i could not manage how to add an image, a frame on top of it and in icon on the left bottom corner. i can make 4 frames with 4 different icons as i have 4 different annotations. bit how can i embed the icon within the frame. – Kassem May 09 '12 at 16:47
  • Did you try subclassing the annotation view? – EmilioPelaez May 09 '12 at 17:37
  • i've updated the question with more information. – Kassem May 09 '12 at 18:01

2 Answers2

13

here in your code

else
{
    MKAnnotationView *annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
    annotationView.canShowCallout = YES;

    //change here
    annotationView.image = [UIImage imageNamed:[NSString stringWithFormat:@"F.png"]];  

    UIImage *frame = [UIImage imageNamed:[NSString stringWithFormat:@"F.png"];
    UIImage *image = theImageInFrameInner;

    UIGraphicsBeginImageContext(CGSizeMake(pin.size.width, pin.size.height));

    [frame drawInRect:CGRectMake(0, 0, frame.size.width, frame.size.height)];
    [image drawInRect:CGRectMake(2, 2, 60, 60)]; // the frame your inner image
    //maybe you should draw the left bottom icon here, 

    //then set back the new image, done
    annotationView.image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [rightButton addTarget:self action:@selector(writeSomething:) forControlEvents:UIControlEventTouchUpInside];
    [rightButton setTitle:annotation.title forState:UIControlStateNormal];

    annotationView.rightCalloutAccessoryView = rightButton;
    annotationView.canShowCallout = YES;
    annotationView.draggable = NO;
    return annotationView;
}
klefevre
  • 8,595
  • 7
  • 42
  • 71
adali
  • 5,977
  • 2
  • 33
  • 40
1
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {


    static NSString *AnnotationViewID = @"annotationViewID";

    MKAnnotationView *annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];

    if (annotationView == nil)
    {
        annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
    }

    UIImage *imgPinBorder = [UIImage imageNamed:@"pinBorder.png"];
    UIImageView *imageViewPinBorder = [[UIImageView alloc] initWithImage:imgPinBorder];
    imageViewPinBorder.center = annotationView.center;
    [annotationView addSubview:imageViewPinBorder];

    UIImage *img = [UIImage imageNamed:@"myIcon.png"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:img];
    imageView.center = annotationView.center;
    [annotationView addSubview:imageView];

    annotationView.annotation = annotation;


    UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [rightButton addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
    [rightButton setTitle:annotation.title forState:UIControlStateNormal];

    annotationView.rightCalloutAccessoryView = rightButton;
    annotationView.canShowCallout = YES;
    annotationView.draggable = NO;



    return annotationView;
}
McDowell
  • 107,573
  • 31
  • 204
  • 267
Sandip Patel - SM
  • 3,346
  • 29
  • 27