[英]Create custom MKAnnotationView with xib
I want to show a custom MKAnnotationView created by XIB on my map but when I load the annotation I don't see it on the map.我想在我的地图上显示由 XIB 创建的自定义 MKAnnotationView,但是当我加载注释时,我在地图上看不到它。
It's the code:这是代码:
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];
}
NOTE: [FPCommonUtils firstViewInNibNamed:NSStringFromClass(self.class) nibOwner:self]
simply return the view related to the xib (this method works very well)注意: [FPCommonUtils firstViewInNibNamed:NSStringFromClass(self.class) nibOwner:self]
只返回与 xib 相关的视图(此方法效果很好)
And this is the code related to mapView:viewForAnnotation:
delegate method这是与mapView:viewForAnnotation:
delegate 方法相关的代码
- (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;
}
}
Any ideas?有任何想法吗?
I have Class of Custom Annotation on MapView Class CallOutAnnotationVifew
:我在 MapView 类CallOutAnnotationVifew
上有自定义注释类:
In .h File,在 .h 文件中,
#import <MapKit/MapKit.h>
@interface CallOutAnnotationVifew : MKAnnotationView
@property (nonatomic,retain)UIView *contentView;
@end
in .m File ,在 .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
Code for Adding Map View in ViewController as below.在 ViewController 中添加地图视图的代码如下。
self.mapView.delegate = self;
self.mapView.showsUserLocation = true;
self.mapView.pitchEnabled = true;
self.mapView.showsBuildings = true;
Code to load Custom Annotation XIB View as below, This is Directly adding to your mapView加载自定义注释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;
}
See OutPut :见输出:
gradle solution dont work's for me, but it's part of the problem. gradle 解决方案对我不起作用,但这是问题的一部分。 I change this parameter in Style files我在样式文件中更改此参数
change Theme.MaterialComponents.Light.NoActionBar
更改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>
Try this solution after try other solutions.尝试其他解决方案后再尝试此解决方案。 Best regards!此致!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.