所以我有一個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%確定它會工作,因爲它會改變顏色,如果我重新啓動應用程序!
編輯:哦,我真的不想開始刪除註釋並重新添加它們。無論如何,這就是我在短期內所做的!
啊,謝謝。我真的不想這樣做,但我已經設法保留標註視圖,所以現在你不能告訴!我會將此標記爲答案,因爲我不知道這是唯一的方法。 –
不,這不是正確答案,請參見下面 –
這不起作用。 – Aggressor