2012-05-27 59 views
13

所以我有一個MKMapView添加了所有我的引腳,並且引腳的顏色取決於是否爲該引腳設置了一個值。當我第一次加載應用程序時,viewForAnnotation被調用並且相應地設置顏色。但是,當我更新引腳的詳細信息(例如位置,標題等)時,我還更新pinColour以查找它不更新。看起來viewForAnnotation在初始添加後沒有再次被調用。強制MKMapView viewForAnnotation更新

我看過類似這樣的很多問題,我可以證實,mapView.delegate = self;

這裏是我的viewForAnnotation代碼:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(MapAnnotation *)annotation 
{ 
    if([annotation class] == MKUserLocation.class) 
     return nil; 

    NSString *pinIdentifier = annotation.identifier; // This is a unique string for each pin and is getting populated every time! 

    MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:pinIdentifier]; 

    if(annotationView == nil) 
     annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pinIdentifier]; 
    else 
     annotationView.annotation = annotation; // Never had this line fire... 

    annotationView.canShowCallout = YES; 
    annotationView.animatesDrop = NO; 
    annotationView.enabled = YES; 
    annotationView.tag = annotation.counter; 

    if(annotation.pinColour == Stopped) // from enum 
     annotationView.pinColor = MKPinAnnotationColorRed; 
    else 
     annotationView.pinColor = MKPinAnnotationColorGreen; 

    UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
    [infoButton addTarget:self action:@selector(mapCalloutButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
    infoButton.tag = annotation.counter; 
    annotationView.rightCalloutAccessoryView = infoButton; 

    return annotationView; 
} 

這裏就是我想補充銷代碼:

CLLocationCoordinate2D annotationCoord; 
annotationCoord.latitude = latestPosition.latitude; 
annotationCoord.longitude = latestPosition.longitude; 

MapAnnotation *annotation = [[MapAnnotation alloc] init]; 
annotation.coordinate = annotationCoord; 
annotation.identifier = theIdentifier; 
annotation.title = theTitle; 
annotation.subtitle = theSubtitle 
annotation.pinColour = [self getPinColour]; 
annotation.counter = theCounter; 

[theMapView addAnnotation:annotation]; 

這裏是我更新引腳的代碼(不同的方法添加):

updatePin = true; 
pinCounter = mapPin.counter; 

CLLocationCoordinate2D annotationCoord; 
annotationCoord.latitude = latestPosition.latitude; 
annotationCoord.longitude = latestPosition.longitude; 
[mapPin setCoordinate:annotationCoord]; 

mapPin.identifier = theIdentifier; 
mapPin.subtitle = theSubtitle; 
mapPin.pinColour = [self getPinColour]; 

我不確定我錯過了什麼。 viewForAnnotation顯然是在工作,它只是在初始添加後才被調用!如果是要調用這個函數,我100%確定它會工作,因爲它會改變顏色,如果我重新啓動應用程序!

編輯:哦,我真的不想開始刪除註釋並重新添加它們。無論如何,這就是我在短期內所做的!

回答

12

由於地圖視圖緩存其註釋的方式,如果需要對其外觀進行更改,則需要刪除並重新添加註釋。一個簡單的刪除&添加是要走的路。沒有緩存失效機制,但這。

+0

啊,謝謝。我真的不想這樣做,但我已經設法保留標註視圖,所以現在你不能告訴!我會將此標記爲答案,因爲我不知道這是唯一的方法。 –

+0

不,這不是正確答案,請參見下面 –

+0

這不起作用。 – Aggressor

36

其實,我不知道這是否適合你,但這是我做到的。

我並不需要從地圖中刪除註釋。我需要做的就是告訴地圖給我一個參數註釋的註解視圖。地圖將返回正確的註釋。從那裏開始,我有一個用於自定義註釋的屬性,以確定它是否爲活動項目,如果是,請顯示正常的針圖像,否則顯示完整的針圖像。

-(void)updateAnnotationImage:(CustomAnnotation *)paramAnnotation 
{ 
    MKAnnotationView *av = [geoMap viewForAnnotation:paramAnnotation]; 

    if (paramAnnotation.active) 
    { 
     av.image = [UIImage imageNamed:@"PinNormal.png"]; 
    } 
    else 
    { 
     av.image = [UIImage imageNamed:@"PinFull.png"]; 
    } 
} 

有點晚了,但希望它可以幫助其他人遇到這個問題。

+0

這是可能的,這是正確的答案。 –

+0

這應該是被接受的答案。謝謝! – lostintranslation

0

雨燕2.1:

我有同樣的問題,並找到一個快速解決方案,觸發此當需要,也將其發送到主線程將是明智的:

var annotationsArray = mapView.annotations 
mapView.removeAnnotations(mapView.annotations) 
mapView.addAnnotations(arrayIncs) 
arrayIncs.removeAll()