2015-09-22 80 views
1

我想在我的地圖上顯示由XIB創建的自定義MKAnnotationView,但是當我加載註釋時,我沒有在地圖上看到它。用xib創建自定義MKAnnotationView

這是代碼:

FPMapPin.h 

#import <MapKit/MapKit.h> 

@interface FPMapPin : MKAnnotationView 

@property (nonatomic,strong) IBOutlet UIImageView *imageView; 
@property (nonatomic,strong) IBOutlet UILabel *labelText; 

@end 

FMMapPin.m 

#import "FPMapPin.h" 

@implementation FPMapPin 

- (instancetype)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 

    if (self) 
    { 
     [self setup]; 
    } 

    return self; 
} 

- (instancetype)initWithCoder:(NSCoder *)aDecoder 
{ 
    self = [super initWithCoder:aDecoder]; 

    if (self) 
    { 
     [self setup]; 
    } 

    return self; 
} 

- (void)setup 
{ 
    UIView* view = [FPCommonUtils firstViewInNibNamed:NSStringFromClass(self.class) nibOwner:self]; 
    view.backgroundColor = [UIColor clearColor]; 
    view.frame = self.bounds; 
    [self addSubview:view]; 
} 

注:[FPCommonUtils firstViewInNibNamed:NSStringFromClass(self.class) nibOwner:self]簡單地返回相關的廈門國際銀行(這種方法效果非常好)

這是代碼視圖與mapView:viewForAnnotation:代表方法

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation 
{ 
    if (annotation == mapView.userLocation) 
    { 
     return nil; 
    } 
    else 
    { 
     NSLog(@"%@",NSStringFromClass([annotation class])); 

     Pin *pin = (Pin*)annotation; 

     FPMapPin *mapPin = [[FPMapPin alloc] initWithAnnotation:annotation reuseIdentifier:@"MapPin"]; 
     mapPin.imageView.image = [UIImage imageNamed:@"pin-white"]; 
     mapPin.labelText.text = [NSString stringWithFormat:@"%li", pin.pinTag]; 

     return mapPin; 
    } 
} 

任何想法?

回答

3

我有自定義註解的類上的MapView類CallOutAnnotationVifew

在.h文件中,

#import <MapKit/MapKit.h> 

@interface CallOutAnnotationVifew : MKAnnotationView 

@property (nonatomic,retain)UIView *contentView; 
@end 
在.m文件

#import "CallOutAnnotationVifew.h" 
#import <QuartzCore/QuartzCore.h> 


#define Arror_height 15 

@interface CallOutAnnotationVifew() 

-(void)drawInContext:(CGContextRef)context; 
- (void)getDrawPath:(CGContextRef)context; 
@end 

@implementation CallOutAnnotationVifew 
@synthesize contentView; 

- (void)dealloc 
{ 
    self.contentView = nil; 
    [super dealloc]; 
} 

- (id)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     self.backgroundColor = [UIColor clearColor]; 
     self.canShowCallout = NO; 
     self.centerOffset = CGPointMake(0, -120); 
     self.frame = CGRectMake(0, 0, 250, 220); 

     UIView *_contentView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height - Arror_height)] autorelease]; 
     _contentView.backgroundColor = [UIColor clearColor]; 
     [self addSubview:_contentView]; 
     self.contentView = _contentView; 

    } 
    return self; 
} 
@end 

代碼在視圖控制器添加地圖視圖如下。

self.mapView.delegate = self; 
self.mapView.showsUserLocation = true; 
self.mapView.pitchEnabled = true; 
self.mapView.showsBuildings = true; 

代碼加載自定義註解XIB查看如下,這是直接添加到您的MapView

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    CallOutAnnotationVifew *anotationCua = [[CallOutAnnotationVifew alloc] initWithAnnotation:annotation reuseIdentifier:@"CallOutAnnotationVifew"]; 

    NSArray *ary = [[NSBundle mainBundle] loadNibNamed:@"Display" owner:self options:nil]; 

    UIView *view1 = [ary objectAtIndex:0]; 

    [anotationCua.contentView addSubview:view1]; 

    return anotationCua; 
} 

見輸出:

enter image description here