只显示一个注释

时间:2013-03-01 05:24:51

标签: iphone objective-c arrays sdk

我的数组中填充了三个对象,地图上只显示了一个注释。有任何想法吗?如果我NSLog某些元素被填充。只是无法显示3个注释。

NSMutableArray *currentBranch = [xmlParser branch];
int counter=[currentBranch count];

NSMutableArray *new = [[NSMutableArray alloc] init];

CLLocationCoordinate2D coordinates;
Mapdets * myAnn;
myAnn = [[Mapdets alloc] init];

[mapview setMapType:MKMapTypeStandard];
[mapview setZoomEnabled:YES];
[mapview setScrollEnabled:YES];

MKCoordinateRegion region = {{0.0, 0.0}, {0.0,0.0 }};
region.center.latitude = -33.86434888;
region.center.longitude = 151.2090236;
region.span.longitudeDelta = 55.80f;
region.span.latitudeDelta = 55.80f;
[mapview setRegion:region animated:YES];


for(int i=0;i<counter;i++)

{
 Mapdets *currentBranch = [[xmlParser branch] objectAtIndex:i];




NSString *title = currentBranch.title;
NSString *subtitle = currentBranch.subtitle;
NSString *lat = currentBranch.lat;  
NSString *lng = currentBranch.lng;     


coordinates.latitude = [lat doubleValue];  
coordinates.longitude = [lng doubleValue];

    myAnn.coordinate = coordinates;
    myAnn.title = title;
    myAnn.subtitle = subtitle;

 NSLog(@"%@",  title);

 [new addObject:myAnn];
}

[mapview addAnnotations:new];

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

只需在for循环中初始化myAnn ..

for(int i=0;i<counter;i++)
{
    Mapdets * myAnn;
    myAnn = [[Mapdets alloc] init];
    Mapdets *currentBranch = [[xmlParser branch] objectAtIndex:i];
    NSString *title = currentBranch.title;
    NSString *subtitle = currentBranch.subtitle;
    NSString *lat = currentBranch.lat;  
    NSString *lng = currentBranch.lng;     

    coordinates.latitude = [lat doubleValue];  
    coordinates.longitude = [lng doubleValue];

    myAnn.coordinate = coordinates;
    myAnn.title = title;
    myAnn.subtitle = subtitle;

    NSLog(@"%@",  title);
   [new addObject:myAnn];
}