2012-04-19 31 views
0

嗯,我有一個地圖,我加載了一些註釋的視圖。一切工作正常,但是當我想刪除這些註釋幷包含其他註釋時,它們不會顯示,直到我移動(滾動)地圖。MKAnnotations沒有顯示,直到我滾動地圖

有什麼建議嗎?

編輯:

好像而不是使用這樣的:

[self updateMap]; 

我必須使用:

[self performSelectorOnMainThread:@selector(updateMap) withObject:nil waitUntilDone:true]; 

作爲更新地圖,添加我的新註釋的方法。

回答

0

嘗試水木清華這樣的:

[mapView removeAnnotations:[mapView annotations]]; 
NSArray *targetArray = /*YOUR ARRAY NAME with object, containing `latitude`, `longitude` and `title` values*/ 
for (id *item in targetArray) { 
    if (item.latitude && item.longitude) { 
     MKPin *pin = [[MKPin alloc] init]; // MKPin is your class which specifies the deledate `MKAnnotation` 
     pin.coordinate = CLLocationCoordinate2DMake(item.latitude, item.longitude); 
     pin.title = item.title; 
     [mapView addAnnotation:pin]; 
    } 
} 
+0

不,我爲每個註釋使用默認視圖,我需要調用'mapView:viewForAnnotation:'方法,但直到我滾動地圖時才調用它 – sergiocg90 2012-04-19 11:43:51

+0

如果使用默認視圖進行註釋,不實施'mapView:viewForAnnotation:'方法。另外,如果你需要,你可以手動運行這個方法。在最後的條件塊中寫下:'[self mapView:mapView viewForAnnotation:pin];' – demon9733 2012-04-19 11:55:00

0

在viewDidLoad中使用:

- (void)viewDidLoad 

{

NSMutableArray* annotations=[[NSMutableArray alloc] init]; 
    MyAnnotation* myAnnotation = [[MyAnnotation alloc] init]; 
myAnnotation.coordinate = theCoordinate; 
    [annotations addObject:myAnnotation]; 
    MKMapRect flyTo = MKMapRectNull; 
for (id <MKAnnotation> annotation in annotations) 
    { 
    NSLog(@"fly to on"); 
     MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate); 
     MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0); 
     if (MKMapRectIsNull(flyTo)) 
     { 
       flyTo = pointRect; 
     } 
     else 
     { 
       flyTo = MKMapRectUnion(flyTo, pointRect); 
     //NSLog(@"else-%@",annotationPoint.x); 
     } 
    } 

// Position the map so that all overlays and annotations are visible on screen. 
mapView.visibleMapRect = flyTo; 

}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 

NSLog(@「welcome into the map view annotation」);

// if it's the user location, just return nil. 
if ([annotation isKindOfClass:[MKUserLocation class]]) 
    return nil; 

// try to dequeue an existing pin view first 
static NSString* AnnotationIdentifier = @"AnnotationIdentifier"; 
MKPinAnnotationView* pinView = [[[MKPinAnnotationView alloc] 
           initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease]; 
pinView.animatesDrop=YES; 
pinView.canShowCallout=YES; 
pinView.draggable = NO; 
pinView.pinColor=MKPinAnnotationColorGreen; 

//button on the right for popup for pins 
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
[rightButton setTitle:annotation.title forState:UIControlStateNormal]; 
[rightButton addTarget:self 
       action:@selector(showDetails:) 
     forControlEvents:UIControlEventTouchUpInside]; 
pinView.rightCalloutAccessoryView = rightButton; 

//zoom button on the left of popup for pins 
UIButton* leftButton = [UIButton buttonWithType:UIButtonTypeContactAdd]; 
[leftButton setTitle:annotation.title forState:UIControlStateNormal]; 
[leftButton addTarget:self 
       action:@selector(zoomToLocation:) forControlEvents:UIControlEventTouchUpInside]; 
pinView.leftCalloutAccessoryView = leftButton; 

UIImageView *profileIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"profile.png"]]; 
pinView.leftCalloutAccessoryView = profileIconView; 
[profileIconView release]; 


return pinView; 

}

希望這有助於!!!!!!!!!