29,028
社区成员
发帖
与我相关
我的任务
分享
.h文件
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface TTCustomAnnotationView : MKAnnotationView {
}
@end
.m文件
#import "TTCustomAnnotationView.h"
@implementation TTCustomAnnotationView
- (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:@"pin.png"]];
[self addSubview:imageView];
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
代理写发
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
if ([annotation isKindOfClass:[TTCustomMapPin class]]) {
static NSString* kPin = @"pin";
TTCustomAnnotationView* pinView = (TTCustomAnnotationView *)
[mapView dequeueReusableAnnotationViewWithIdentifier:kPin];
if (!pinView) {
pinView = [[TTCustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:kPin];
[pinView setDraggable:YES];
}
return pinView;
}
return nil;
}