2012-05-27 29 views
13

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!

Trả lời

12

Do cách chế độ xem bản đồ lưu trữ chú thích của nó, bạn CẦN phải xóa và thêm lại chú thích nếu bạn cần thực hiện thay đổi đối với giao diện của nó. Một đơn giản loại bỏ & thêm là con đường để đi. Không có cơ chế làm mất hiệu lực bộ nhớ cache nhưng điều này.

+0

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. –

+0

Không, đó không phải là câu trả lời đúng, xem bên dưới –

+0

Điều này không có tác dụng. – Aggressor

36

Thực ra, tôi không biết điều này có hiệu quả với bạn hay không nhưng đây là cách tôi đã làm.

Tôi không cần xóa chú thích khỏi bản đồ. Tất cả những gì tôi cần làm là yêu cầu bản đồ cung cấp cho tôi chế độ xem chú thích cho chú thích tham số. Bản đồ sẽ trả lại chú thích chính xác. Từ đó, tôi có thuộc tính cho chú thích tùy chỉnh của mình để xác định xem đó có phải là một mục đang hoạt động hay không, nếu có, hiển thị hình ảnh pin bình thường, hiển thị hình ảnh pin đầy đủ khác.

-(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"]; 
    } 
} 

Bit trễ nhưng hy vọng nó sẽ giúp những người khác gặp phải vấn đề này.

+0

Điều này là có thể và đây là cách chính xác .. 1 cho câu trả lời. –

+0

Điều này thực sự phải là câu trả lời được chấp nhận. Cảm ơn! – lostintranslation

0

Swift 2.1:

tôi đã cùng một vấn đề, và tìm thấy một giải pháp nhanh chóng, gây ra điều này khi cần thiết, cũng gửi nó đến chủ đề chính sẽ là khôn ngoan:

var annotationsArray = mapView.annotations 
mapView.removeAnnotations(mapView.annotations) 
mapView.addAnnotations(arrayIncs) 
arrayIncs.removeAll() 
Các vấn đề liên quan