自定义MKAnnotation未显示在屏幕上

时间:2013-06-24 14:40:38

标签: ios objective-c mkmapview

所以我想在我的mkmap视图中添加自定义注释。我已将其追溯到我的自定义注释类中的一个问题(见下文)。有人看到我的代码有问题吗?

//// .h文件/////

@interface AddressAnnotation : MKAnnotationView 

@property (nonatomic, strong) NSString *address;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, strong) UIImage *image;


-(id)initWithCoordinate:(CLLocationCoordinate2D)coord;
-(id)initWithCoordinate:(CLLocationCoordinate2D)coord andAddress:(NSString *)address;

@end

/// .m ///////////

@implementation AddressAnnotation

@synthesize image = _image;

-(id)initWithCoordinate:(CLLocationCoordinate2D)coord {

return [self initWithCoordinate:coord andAddress:nil];

}


-(id)initWithCoordinate:(CLLocationCoordinate2D)coord andAddress:(NSString *)address {

if (self = [super init]) {
    self.coordinate = coord;
    self.address = address;
}
return self;
}



@end

我还应该声明我在我的.h中试过这个但是没有帮助。

@interface AddressAnnotation : MKAnnotationView <MKAnnotation>

我也知道我的注释中的代码,因为我将注释类型更改为我知道的注释类型并将其放在地图上。

===更新================ 根据您的要求提供更多代码

以下是我的viewForAnnotation代码的两个示例。最重要的是 NOT 工作,底部 IS 工作。

///不起作用/////

else if([annotation isKindOfClass:[AddressAnnotation class]]){

    static NSString *identifier = @"addressLocation";
    AddressAnnotation *addAnno = (AddressAnnotation *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

    if (!addAnno) {
        addAnno = [[AddressAnnotation alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
    }

    addAnno.image = [UIImage imageNamed:@"Map_pin.png"];
    addAnno.bounds = CGRectMake(0, 0, 32, 39); //initial bounds (default)
    return addAnno;

///正在工作/////

else if([annotation isKindOfClass:[AddressAnnotation class]]){

    static NSString *identifier = @"addressLocation";
    HGMovingAnnotationView *addAnno = (HGMovingAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

    if (!addAnno) {
        addAnno = [[HGMovingAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
    }

    addAnno.image = [UIImage imageNamed:@"Map_pin.png"];
    addAnno.bounds = CGRectMake(0, 0, 32, 39); //initial bounds (default)
    return addAnno;
}

2 个答案:

答案 0 :(得分:0)

不确定是什么问题所以我只是将AddressAnnotation作为一个有效的子类。下面的课程。

@interface HGMovingAnnotationView : MKAnnotationView

@interface AddressAnnotation : HGMovingAnnotationView 

答案 1 :(得分:0)

在示例中创建另一个类AuxLocation。

AuxLocation.h

 #import < Foundation/Foundation.h>

 #import < mapKit/MKAnnotation.h>

@interface AuxLocation : NSObject
{

    CLLocationCoordinate2D coordinate;
    NSString *title;
    NSString *subtitle;
}

@property(nonatomic,assign)CLLocationCoordinate2D coordinate;

@property(nonatomic,copy) NSString *title;

@property(nonatomic,copy) NSString *subtitle;

@end

然后是AuxLocation.m

**#import "AuxLocation.h"**

@implementation AuxLocation

@synthesize coordinate,title,subtitle;

@end

然后在要显示注释的类中

MyClass.h

@interface MyClass : UIViewController
{

    AuxLocation *annotation;   


}

MyClass.m

- (void)viewDidLoad
{

    [super viewDidLoad];

    // Do any additional setup after loading the view.
    [super viewDidLoad];

    annotation = [[AuxLocation alloc] init];

    annotation.title=@"Antonella Pino Diseño Interior";

    annotation.coordinate=region.center;

    [mapview addAnnotation:annotation];   

}

希望这个结果很有用。