2011-07-03 4 views
2

비동기 요청이 완료된 후 주석 상태에 대한 정보로 사용자 정의 MKAnnotationView 이미지를 업데이트하는 방법을 찾는 데 문제가 있습니다. 후MapKit 업데이트 주석 이미지

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

    static NSString *identifier = @"EstacionEB"; 
    if ([annotation isKindOfClass:[EstacionEB class]]) { 
     EstacionEB *location = (EstacionEB *) annotation; 

     CustomPin *annotationView = (CustomPin *) [_mapita dequeueReusableAnnotationViewWithIdentifier:identifier]; 
     if (annotationView == nil) { 
      annotationView = [[CustomPin alloc] initWithAnnotation:annotation reuseIdentifier:identifier]; 
     } else { 
      annotationView.annotation = annotation; 
     } 

     UIImage * image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.png", [location elStatus]]]; 

     annotationView.enabled = YES; 
     annotationView.canShowCallout = YES; 
     annotationView.image = image; 

     NSDictionary *temp = [[NSDictionary alloc] 
           initWithObjects:[NSArray arrayWithObjects:annotationView, location, nil] 
           forKeys:[NSArray arrayWithObjects:@"view", @"annotation", nil] 
           ]; 
     //This array is synthesized and inited in my controller's viewDidLoad 
     [self.markers setObject:temp forKey:location.eid]; 
     return annotationView; 
    } 

    return nil;  
} 

약간, 내가 요청을하고 결과,있는 NSDictionary로, 나는 두 요소에 null를 돌려 다음을 수행하려고 해요 : 지금까지, 나는이이

- (void)updateStation:(NSString *)eid withDetails:(NSDictionary *)details 
{ 
    NSInteger free = [details objectForKey:@"free"]; 
    NSInteger parkings = [details objectForKey:@"parkings"]; 

    NSDictionary *storedStations = [self.markers objectForKey:eid]; 

    CustomPin *pin = [storedStations objectForKey:@"view"]; //nil 
    EstacionEB *station = [referencia objectForKey:@"annotation"]; //nil as well 

    [station setSubtitle:free]; 

    NSString *status; 
    if(free==0){ 
     status = @"empty"; 
    } else if((free.intValue>0) && (parkings.intValue<=3) ){ 
     status = @"warning"; 
    } else { 
     status = @"available"; 
    } 
    UIImage * image = [UIImage imageNamed:[NSString imageWithFormat:@"%@.png", status]]; 
    pin.image = image; 
} 

이 (내가 붙여 정확하게 모든 중상 가정) 오류를 제공하지 않지만, NSMutableDictionary 포함해야하는 내 사용자 정의 MKAnnotationView과 MKAnnotation하지만 심지어 내가 요청하기 전에 그들 모두를 로그인 할 때 완료하고 나타나는 모두 올바르게, 언제 요구 est는 마치 MKAnnotationView와 MKAnnotation이 내가 예상했던 것과 다르지 않으므로 완성됩니다. 따라서 주석을 수정하여 이미지를 변경하거나 주석보기를 업데이트 할 수 없습니다.

모든 아이디어를 얻을 수 있습니다.

답변

6

왜 마커 배열에서 nil 값을 얻는 지 잘 모르겠습니다 (특히 특수 효과의 경우). 그러나 이와 같은 주석 뷰에 대한 참조 저장은 권장하지 않습니다.

대리자 메서드는 필요할 때마다지도보기에서 호출 할 수 있으며보기 개체가 한 호출에서 다음 호출로 변경 될 수 있습니다. 각 주석에 대해 동일한 재사용 식별자를 사용하는 주석 뷰를 다시 사용하기 때문에 동일한 뷰 객체를 나중에 다른 주석에 다시 사용할 수있는 가능성도 있습니다. 그 eid는 를 업데이트하는 것과 일치하는 경우

  • 지도보기의 annotations 배열
  • 주석 유형 EstacionEB의 경우을 반복 한 다음 확인 :

    대신, updateStation에, 나는 다음과 같은 제안

  • 주석의 subTitleelStatus 속성을 업데이트하십시오 (viewForAnnotation 대리자 방법으로 이미지를 설정하는 데 사용되므로 elStatus을 업데이트하는 것이 중요합니다)
  • 뷰의 image 속성이

이보기

  • 업데이트 (이 대리자 방법 mapView:viewForAnnotation: 같은 하지입니다)지도보기의 viewForAnnotation: 인스턴스 메서드를 호출하여 주석의 현재보기를 얻을 비슷한 예는 other related question입니다.

  • +0

    감사합니다. 나는 당신의 권고를 따르고 나중에 다시보고 할 것입니다. – Roberto

    +0

    이것은 정확히 내가해야 할 일이다. 정말 고마워! – Roberto

    관련 문제