MapKit中的多个Pin字幕注释?

时间:2015-03-09 11:44:57

标签: ios objective-c mapkit mkannotationview

我正在使用坐标和其他信息解析csv文件中的数据。我绘制了引脚,我想知道如何在引脚的字幕对象中添加其他数据。这是我的方法(我试图添加的对象是“温度”):

ViewController.h

    NSString * latitude = [infos objectAtIndex:1];
    NSString * longitude = [infos objectAtIndex:2];
    NSString * description =[infos objectAtIndex:3];
    NSString * address = [infos objectAtIndex:4];
    NSString * temperature = [infos objectAtIndex:5];

    CLLocationCoordinate2D coordinate;
    coordinate.latitude = latitude.doubleValue;
    coordinate.longitude = longitude.doubleValue;
    Location *annotation = [[Location alloc] initWithName:description address:address temperature:temperature coordinate:coordinate] ;
    [mapview addAnnotation:annotation];

Location.h

@interface Location : NSObject  {
NSString *_name;
NSString *_address;
NSString *_temperature;
CLLocationCoordinate2D _coordinate;
}

@property (copy) NSString *name;
@property (copy) NSString *address;
@property (copy) NSString *temperature;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

- (id)initWithName:(NSString*)name address:(NSString*)address temperature:     
  (NSString*)temperature coordinate:(CLLocationCoordinate2D)coordinate;

Location.m

@implementation Location
@synthesize name = _name;
@synthesize address = _address;
@synthesize coordinate = _coordinate;
@synthesize temperature = _temperature;

- (id)initWithName:(NSString*)name address:(NSString*)address temperature:(NSString*)temperature coordinate:(CLLocationCoordinate2D)coordinate {
if ((self = [super init])) {
    _name = [name copy];
    _address = [address copy];
    _temperature = [temperature copy];
    _coordinate = coordinate;
}
return self;
}

- (NSString *)title {
if ([_name isKindOfClass:[NSNull class]])
    return @"Unknown charge";
else
    return _name;
}

- (NSString *)subtitle {
return _address;
return _temperature;

}

正如你所看到的,我试图在“副标题”方法中添加“温度”对象,遗憾的是,它没有显示在注释中。现在我正在尝试使用温度对象,但我需要绘制更多,所以我需要一种方法来添加尽可能多的对象。

1 个答案:

答案 0 :(得分:0)

subtitle方法中:

- (NSString *)subtitle {
return _address;
return _temperature;

}
return _address;行执行后,

不执行任何操作。

return语句如何工作(执行立即返回给调用者)。

不要将代码的排列与用户界面中显示的变量的显示方式混淆。

此外,MapKit中的内置标注仅支持titlesubtitle各一条短线。试图在每个属性中显示多行将导致失望。

所以你可以这样做:

- (NSString *)subtitle {
    return _address;
}

或:

- (NSString *)subtitle {
    return _temperature;
}

或:

- (NSString *)subtitle {
    //display "address, temperature"...
    return [NSString stringWithFormat:@"%@, %@", _address, _temperature];
}


如果您必须显示比默认值更详细的标注,则您需要编写可能变得复杂的自定义标注视图。如果感兴趣,请参阅Custom MKAnnotation callout bubble with buttonShow custom and default callout views on different pins with pin clustering on map view以了解一些示例。

<小时/> 顺便说一句,无关,但Location类应该声明它实现了MKAnnotation协议。这将帮助您和编译器避免或显示适当的警告并自行记录代码作为附带好处:

    @interface Location : NSObject<MKAnnotation>  {