MKAnnotation - 没有出现多个注释

时间:2013-06-12 23:54:01

标签: iphone ios mapkit mkannotation

在我的应用程序出现问题后,我为MKMapView编写了一个测试应用程序。我的问题是我不能在地图上放置多个引脚。在viewDidLoad:方法中使用此代码,应该在指定坐标的对角线上显示10个引脚:

Annotation *annotation = [[Annotation alloc] init];
NSMutableArray *array = [[NSMutableArray alloc] init];
for (int i = 0; i < 10; i++) {
    annotation.title = [NSString stringWithFormat:@"Pin%i", i];
    annotation.subtitle = [NSString stringWithFormat:@"%i", i];
    annotation.coordinate = CLLocationCoordinate2DMake(37.41181 + (i * .1), -122.11809 + (i * .1));
    [array addObject:annotation];
}
[map addAnnotations:array];

Annotation只是:

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface Annotation : NSObject <MKAnnotation>

@property(strong, nonatomic) NSString *title;
@property(strong, nonatomic) NSString *subtitle;
@property(nonatomic) CLLocationCoordinate2D coordinate;

-(id)init;

@end


@implementation Annotation
@synthesize title, subtitle, coordinate;

-(id)init {
    self = [super init];
    return self;
}

@end

而不是看到10个引脚,我只看到我的“当前位置”和一个很远的引脚,它的标题和副标题是“Pin9”和“9”。它似乎只添加了数组中最新的元素。

有人能看到发生了什么吗? 感谢。

2 个答案:

答案 0 :(得分:1)

必须在for循环中分配注释。

Annotation * annotation = [[Annotation alloc] init];

答案 1 :(得分:0)

稍微摆弄一下后,我发现如果你放置这条线就行了:

Annotation *annotation = [[Annotation alloc] init];

for循环中,不在外面。 哦,好好生活和学习。