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
本文详细介绍了如何在MKMapView中使用Objective-C自定义标记大头针,包括大头针的图片设置、颜色透明化以及标记的拖动功能实现。
2872

被折叠的 条评论
为什么被折叠?



