2011-07-03 11 views
2

非同期要求が完了した後、カスタムMKAnnotationView画像を更新する方法を見つけ出すのに問題があります。注釈のステータスに関する情報が含まれています。後MapKit update注釈画像

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

これはエラー(私が貼り付けられたとすべてを正しくtraduced仮定)を立ち上げるありませんが、NSMutableDictionary私のカスタムMKAnnotationViewとMKAnnotationの両方が含まれているべきであるが、要求が完了すると、それが表示される前に、私はそれらをすべてログに記録しても正しく、要求があるとき私はMKAnnotationViewとMKAnnotationの両方が私が期待したものではないので、画像を変更したり注釈ビューを更新するために注釈を変更することはできません。

いずれかのアイデアをいただければ幸いです!

答えて

6

なぜマーカー配列からの値がゼロになっているのかわかりません(特に注釈の場合)。しかし、そのような注釈ビューへの参照の保存はお勧めしません。

viewForAnnotationデリゲート・メソッドは、マップ・ビューによって必要と思われ、ビュー・オブジェクトがあるコールから次のコールに変わる可能性があると思うときはいつでも呼び出すことができます。各アノテーションに同じ再利用識別子を使用してアノテーションビューを再利用しているため、後で別のアノテーションに同じビューオブジェクトを再利用できる可能性もあります。

代わりに、updateStationに、私は次のことをお勧め:マップビューのannotations配列

    • ループ注釈がタイプEstacionEBであれば、そのeid
    • を更新されているものと一致するかどうかを確認
    • アノテーションのsubTitleelStatusのプロパティを更新します(viewForAnnotationデリゲートメソッドでイメージを設定するために使用されるため、elStatusを更新することが重要です)

    これを参照してくださいビューのimageプロパティ更新

  • (これは、デリゲートメソッドmapView:viewForAnnotation:としてない同じである)、マップビューのviewForAnnotation:インスタンスメソッドを呼び出すことにより、注釈の現在のビューを取得同様の例についてはother related questionである。

  • +0

    ありがとうございました!私はあなたの推薦に従い、後でまた報告します。 – Roberto

    +0

    これはまさに私がする必要がありました、ありがとうございました! – Roberto

    関連する問題