2011-10-08 55 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;是什麼定義每個數組對象的緯度和長度?我會試穿,謝謝! –