MapKit 中的多个 Pin 字幕注释?

Multiple Pin subtitle annotations in MapKit?

我正在从一个包含坐标和其他信息的 csv 文件中解析数据。我绘制了图钉,我想知道如何在图钉的字幕对象中添加其他数据。这是我的方法(我要添加的对象是 "temperature"):

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;

}

如您所见,我尝试在 "subtitle" 方法中添加 "temperature" 对象,不幸的是,它没有显示在注释中。现在我正在尝试使用温度对象,但我需要绘制更多,所以我需要一种方法来添加尽可能多的对象。

subtitle方法中:

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

}

return _address; 行之后的任何内容都不会执行。

这就是 return 语句的工作原理(立即向调用者执行 returns)。

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

此外,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 button and Show custom and default callout views on different pins with pin clustering on map view 中的几个示例。


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

    @interface Location : NSObject<MKAnnotation>  {