12

このようなカスタム MKAnnotationView を設計する方法について、同様のソリューションを探していました。

ここに画像の説明を入力

MKAnnotation をサブクラス化し、F.png という名前の画像を追加することができました。F.png は、図に示すようにフレーム画像です。私が欲しいのは、内側の画像を追加することです。(私が描いた絵では青く着色されています)

- (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;
}
4

2 に答える 2

12

ここにあなたのコードで

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;
}
于 2012-05-10T01:12:47.567 に答える
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;
}
于 2012-06-22T12:59:05.947 に答える