我想在我的地图上显示由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;
}
}
有什么想法吗?
答案 0 :(得分:4)
我在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;
}
见OutPut:
答案 1 :(得分:0)
渐变解决方案对我不起作用,但这是问题的一部分。 我在样式文件中更改了此参数
更改Theme.MaterialComponents.Light.NoActionBar
<style name="AppTheme" parent="Theme.MaterialComponents.Light">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimary</item>
<item name="colorAccent">@color/colorPrimary</item>
<item name="android:windowTranslucentStatus"
tools:ignore="NewApi">false</item>
</style>
尝试其他解决方案后再尝试此解决方案。 最好的问候!