MKMapView中有一个代理方法- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation可以返回的就是一个view就是大头针;
Object C 代码:
.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;
}
欢迎访问:http://www.orietech.com
Wordpress: http://orietech.wordpress.com