更改注释引脚的图像

时间:2013-10-24 08:18:26

标签: ios mapkit

这就是我在MapKit上添加注释的方法:

 MKPointAnnotation *point = [[MKPointAnnotation alloc] init];

    point.coordinate = zoomLocation;
    point.title = @"Where am I?";
    point.subtitle = @"I'm here!!!";


    [self.mapView addAnnotation:point ];

如何更改注释的图像?

2 个答案:

答案 0 :(得分:1)

创建自定义注记类。

CustomAnnotation.h

中的

#import <MapKit/MapKit.h>

@interface CustomAnnotation : MKAnnotationView

@end
CustomAnnotation.m

中的

#import "CustomAnnotation.h"

@implementation CustomAnnotation

-(id)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier    {

    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
    if (self != nil) {
        CGRect frame = self.frame;
        frame.size = CGSizeMake(46.0, 49.0);
        self.frame = frame;
        self.backgroundColor = [UIColor clearColor];
        self.centerOffset = CGPointMake(-5, -5);
    }
    return self;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    [[UIImage imageNamed:@"IMAGE_NAME"] drawInRect:rect];

}

在我们的mapview委托方法 viewForAnnotation

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


    static NSString *defaultPinId = @"Pin";
    CustomAnnotation *pinView = (CustomAnnotation *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinId];

    if (pinView == nil) {

        pinView = [[CustomAnnotation alloc]initWithAnnotation:annotation reuseIdentifier:defaultPinId];

    }
    else    {
        pinView.annotation = annotation;
    }

    return pinView;

}

答案 1 :(得分:0)

试试这个,

MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
[[self.mapView viewForAnnotation:point] setTag:1];
UIImage *image = [UIImage imageNamed:@"pinIcon.png"];
[[self.mapView viewForAnnotation:point] setImage:image];