Vì vậy, tôi có một MKMapView với tất cả các chân của tôi được thêm vào, và màu sắc của pin phụ thuộc vào việc một giá trị được thiết lập cho pin đó. Khi tôi tải ứng dụng lần đầu tiên, viewForAnnotation
được gọi và các màu được đặt tương ứng. Tuy nhiên, khi tôi cập nhật chi tiết của pin (chẳng hạn như vị trí, tiêu đề, v.v ...), tôi cũng cập nhật pinColour để tìm thấy nó không cập nhật. Có vẻ như viewForAnnotation
không được gọi lại sau lần thêm ban đầu.Force MKMapView viewForAnnotation để cập nhật
Tôi đã đọc rất nhiều câu hỏi tương tự như này và tôi có thể khẳng định rằng mapView.delegate = self;
Đây là mã viewForAnnotation
tôi:
- (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;
}
Đây là mã nơi tôi thêm pin:
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];
Đây là mã mà tôi cập nhật ghim (phương pháp khác để thêm):
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];
Tôi không chắc chắn những gì tôi đang thiếu. viewForAnnotation
rõ ràng là hoạt động, nó chỉ không bao giờ được gọi sau khi thêm ban đầu! Nếu nó được gọi là chức năng này tôi chắc chắn 100% nó sẽ làm việc vì nó thay đổi màu sắc nếu tôi khởi động lại ứng dụng!
EDIT: Ồ và tôi thực sự không muốn bắt đầu xóa chú thích và thêm lại chú thích. Đó là những gì tôi đang làm trong ngắn hạn anyway!
Ah cảm ơn. Tôi không thực sự muốn làm điều đó theo cách đó nhưng tôi đã quản lý để giữ lại chế độ xem chú thích vì vậy bây giờ bạn không thể nói được! Tôi sẽ đánh dấu đây là câu trả lời vì tôi không biết rằng đây là cách duy nhất. –
Không, đó không phải là câu trả lời đúng, xem bên dưới –
Điều này không có tác dụng. – Aggressor