2013-03-11 4 views
5

私はマップビューコントローラ(UIViewController、MKMapView)をデリゲート(HCIResultMapViewController)とともに使用しています。mkannotationのカスタムデータ変数

この部分には次の機能があります。

1)。私は等のタイトル、サブタイトルなどの基本的なエンティティと一緒に他の人の詳細を関連付けることができるように私は、私のカスタムは、NSObjectのを作っ使いたい

は、そこで自分のニーズに応じて、私は

- (void)viewDidLoad { 
[super viewDidLoad]; 
// Do any additional setup after loading the view. 

_houseList = [[_resultList objectForKey:@"result"] objectForKey:@"listings"]; 

// NSLog([_houseList description]); 

int i = 0; 

for (NSDictionary *house in _houseList) { 

    HCIAnnotationViewController *annotation = [[HCIAnnotationViewController alloc] 
            initwithHouse:house]; 
    [_mapView addAnnotation:annotation]; 
    // NSLog(@"asdjhasdjsajdhaksdjghasdasdjahsdahskvdka"); 
    self.currIdentifier = i; 
    i++; 
} 

[_mapView setShowsUserLocation:NO]; 
} 
HCIResultMapViewController

を次のようにコーディング

他のデリゲート機能

-(void) mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control { 

for (NSObject<MKAnnotation> *annotation in _mapView.selectedAnnotations) { 
    NSLog(@"hellomaster"); 
    NSLog(annotation.title); 
    if ([annotation isKindOfClass:[HCIAnnotationViewController class]]) { 
     NSLog(@"hellomaster"); 
    } 
} 

最後の1

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

NSString *identifier = @"currIdentifier"; 

    MKPinAnnotationView *annotationView = 
    (MKPinAnnotationView *)[_mapView dequeueReusableAnnotationViewWithIdentifier:identifier]; 

    if (annotationView == nil) { 
     annotationView = [[MKPinAnnotationView alloc] 
          initWithAnnotation:annotation 
          reuseIdentifier:identifier]; 
    } else { 
     annotationView.annotation = annotation; 
    } 

    annotationView.enabled = YES; 
    annotationView.canShowCallout = YES; 
annotationView.tag = self.currIdentifier; 

    // Create a UIButton object to add on the 
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
    [rightButton setTitle:annotation.title forState:UIControlStateNormal]; 
    [annotationView setRightCalloutAccessoryView:rightButton]; 

/* 
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeInfoLight]; 
    [leftButton setTitle:annotation.title forState:UIControlStateNormal]; 
    [annotationView setLeftCalloutAccessoryView:leftButton]; 
    */ 
    return annotationView; 
} 

しかし、私はクラスの等価性が失敗することがわかります。私が間違っていることを教えてくれますか?

単純な言葉で言いますと、必要なときにいつでも取得できるように、データ(NSDictionary *)とアノテーションを一緒に送信するにはどうすればいいですか?

この質問には繰り返し質問があります。私はグーグルなど多くの質問を試みましたが、これに適した解決策を見つけることができませんでした。

+0

あなたはカスタムアノテーションを作成したいと思います。タイトル、サブタイトルのようなものですか? – iPatel

+0

番号。私の注釈には、タイトル、サブタイトルのようなエンティティとともにNSDictionary *が含まれています。 –

答えて

1

を私が実装するための正確な方法を見つけることができませんでしたカスタムデータ変数、または私のクラス等価が失敗する理由。しかし、私はこのような状況に終わる方法を考え出しました。これはメソッドで重複しているかもしれませんが、まだ魅力のように動作します。

したがって、制御ボタンにタグシステムを使用することです。注釈を追加するためにマップビューにメッセージを送信するたびに、直ちにviewforannotationメソッドが呼び出されることがわかりました。私は配列を反復処理しているので、私はコントロールのボタンのタグを設定するインデックスのグローバルポインタを維持しました。後でボタンをクリックすると、タグを取得して、それを使用して目的のオブジェクトを取得します。

他の人には、代替または直接解決策があります。投稿してください。

1

NSMutableDictionaryのインスタンスをNSStringに設定することもできます。
カスタムAnnotationView作成:

#import <MapKit/MapKit.h> 

@interface AnnotationView : MKPlacemark 

@property (nonatomic, readwrite, assign) CLLocationCoordinate2D coordinate; 

@property (nonatomic, strong) NSString *title; //Here You cam set `NSMutableDictionary` instand of `NSString` 
@property (nonatomic, strong) NSString *subtitle; //Here You cam set `NSMutableDictionary` instand of `NSString` 


@end 

そして.m file

#import "AnnotationView.h" 

@implementation AnnotationView 

- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate addressDictionary:(NSDictionary *)addressDictionary 
{ 
    if ((self = [super initWithCoordinate:coordinate addressDictionary:addressDictionary])) 
    { 
     self.coordinate = coordinate; 
    } 
    return self; 
} 

@end 

//使用注釈であなたの関連.m file#import "AnnotationView.h"を追加します。

CLLocationCoordinate2D pCoordinate ; 
pCoordinate.latitude = LatValue; 
pCoordinate.longitude = LanValue; 

// Create Obj Of AnnotationView class 

AnnotationView *annotation = [[AnnotationView alloc] initWithCoordinate:pCoordinate addressDictionary:nil] ; 

    annotation.title = @"I m Here"; 
    annotation.subtitle = @"This is Sub Tiitle"; 

[self.mapView addAnnotation:annotation]; 
+0

要素(NSDictionary *)を持つカスタム注釈ビューを使用することをお勧めしますか?しかし、それは私のカスタム注釈がクラスの等価性に失敗する理由を私に説明していません。これを私に説明できますか? –