创建一个MKAnnotationView的子类,用于修改大头针的图片
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface TTCustomAnnotationView : MKAnnotationView
@end
- (id)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
if (self) {
self.backgroundColor = [UIColor clearColor];
//大头针的图片
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[imageView setImage:[UIImage imageNamed:@"popoverArrowUp@2x.png"]];
[self addSubview:imageView];
}
return self;
}
在mapview的委托函数中,直接使用这个view初始化大头针,就可以了
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
static NSString* kPin = @"pin";
TTCustomAnnotationView* pinView = (TTCustomAnnotationView *)
[mapView dequeueReusableAnnotationViewWithIdentifier:kPin];
if (!pinView) {
pinView = [[TTCustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:kPin];
[pinView setDraggable:YES];
}
return pinView;
}