【iPhone Demo】地图自定义大头针

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;
}

【iPhone Demo】地图自定义大头针_第1张图片

欢迎访问:http://www.orietech.com

Wordpress: http://orietech.wordpress.com



你可能感兴趣的:(c,object,iPhone,Class,interface)