使用xib创建自定义MKAnnotationView

问题描述 投票:1回答:2

我想在地图上显示由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;
    }
}

有什么想法吗?

ios objective-c iphone mkmapview mkannotationview
2个回答
4
投票

我在MapView类CallOutAnnotationVifew上具有自定义注释类:


0
投票

Gradle解决方案对我不起作用,但这是问题的一部分。我在样式文件中更改此参数

© www.soinside.com 2019 - 2024. All rights reserved.