2011-10-08 51 views
0

下面的代码是我到目前为止所使用的代码,它正确地遍历数组中的每个对象,但是当我尝试使它们全都显示在一张地图上时只会将数组中最后一个元素添加到地图中,而不是全部20个左右我想要显示。如何在基于数组的mapview上有多个注释

self.clientTable = [ClientDatabase database].clientTable; 

    ClientTable *info = nil; 
    [_nameLabel setText:info.name]; 
    [_stateLabel setText:info.state]; 

    //change the string to doubles for the map GPS co-ordinates 
    double latDouble = [info.latMap doubleValue]; 
    double longDouble = [info.longMap doubleValue]; 

    NSLog(@"%d",[self.clientTable count]); 

    int countArray = [self.clientTable count]; 

    for (int i=0;i<countArray;i++) { 

     info = [self.clientTable objectAtIndex:i]; 
     info.uniqueId=i; 
     NSLog(@" i = %d ; id = %d %@",i, info.uniqueId, info.name); 

     //set up the map 
     [super viewDidLoad]; 
     [mapView setMapType:MKMapTypeStandard]; 
     [mapView setZoomEnabled:YES]; 
     [mapView setScrollEnabled:YES]; 
     MKCoordinateRegion region = {{0.0,0.0},{0.0,0.0}}; 

     region.center.latitude = latDouble; 
     region.center.longitude = longDouble; 
     region.span.longitudeDelta =0.02; //degrees of acuracy, most precise best for this time 
     region.span.latitudeDelta =0.02; //degrees of accuracy 

     [mapView setRegion:region animated:YES]; 

     // set up the annotation point 

     AllMap *annotationPoint = [[AllMap alloc] init]; 
     annotationPoint.title = info.name; 
     annotationPoint.subtitle = info.state; 
     annotationPoint.coordinate = region.center; 
     [mapView addAnnotation:annotationPoint]; 
     annotationPoint.isAccessibilityElement=YES; 
     //show annotation by default 
     [mapView selectAnnotation:annotationPoint animated:YES]; 

     [mapView setDelegate:self]; 

    } 

对不起,如果代码是垃圾,我是新的iPhone编程。

在此先感谢:d

回答

0

你为什么要设置在其中创建注释环路内的地图吗?

这里是一个古老的一篇博客中,但它涵盖了基础知识,并应该让你回到正轨

+0

我这样做,因为我认为如果我为每个ID创建一个注释,那么它会创建一个新的每个循环迭代,但它只是覆盖它,直到最后一个达到然后显示。 你也忘了附上一个链接。 谢谢! –

1

它看起来像你打电话[super viewDidLoad]for循环,这可能是重置的MapView的注释数组中。这种方法只能被调用一次,所以如果你在for声明之前移动它,你可能会得到更好的结果。

+0

正如Aaron指出的那样,您在'for'循环中设置MapView的其他内容实际上并不需要执行20次 - 但是这是对[super viewDidLoad]的调用,注释。实际上,//设置地图和设置注释点之间的所有内容都应该移到for循环之外。 –

+0

但是两行:region.center.latitude = latDouble;和region.center.longitude = longDouble;是什么定义每个数组对象的纬度和长度?我会试穿,谢谢! –