我想在地图上显示由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]
仅返回与xib相关的视图(此方法效果很好)这是与
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;
}
}
有任何想法吗?
最佳答案
我在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
如下在ViewController中添加地图视图的代码。
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;
}
参见输出:
关于ios - 使用Xib创建自定义MKAnnotationView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32714221/