2012-04-16 14 views
1

MKMapViewオブジェクトを使用しています。ピンを作成して地図に配置できます。私が今やっていることは、ピンから情報を得ることです。たとえば、私の場合iOSのマップピンから位置を取得する方法

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

私はピンを作成して地図上に設定します。ユーザーがピンをクリックしたときに使用するボタンも作成しています。そのすべてがうまくいいです。さて、私の質問は、そのボタンがクリックされたときです。そのクリックされたピンからどのように情報を取得するのですか?以下は、私が現在どのようにセットアップしているかの小さなサンプルコードです。アドバイスをいただければ幸いです。

- (MKAnnotationView*) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation 
{ 
    MKPinAnnotationView *annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"location"]; 
    annView.pinColor = MKPinAnnotationColorRed; 
    annView.animatesDrop = YES; 
    annView.canShowCallout = YES; 
    annView.calloutOffset = CGPointMake(-5, 5); 

    UIButton* callout = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
    [callout addTarget:self action:@selector(loadDetails:) forControlEvents:UIControlEventTouchUpInside]; 
    annView.rightCalloutAccessoryView = callout; 
    return annView; 
} 

- (void)loadDetails:(id)sender 
{ 
    //TODO: Get pin coordinates or address and match it back to an array of available address to show details in a new view 
} 

何かアドバイスが大幅にあなたはid<MkAnnotationView>の性質を持っているMKPinAnnotationViewから派生クラスを作成する必要があり

答えて

0

の代わりにあなたのコールアウトのターゲットを設定し、デリゲートメソッド

mapView:annotationView:calloutAccessoryControlTapped: 

ザ・への対応annotationViewは、座標がannotationのプロパティを持ちます。

+0

これは私のためにうまくいきました。私が持っている質問だけですが、私はまだボタンを作成する必要がありますか?ボタンが実際に設定されていない場合や、何かが無駄であると感じるだけです。 – Seb

+0

あなたは '[UIButton buttonWithType:UIButtonTypeDetailDisclosure]'を使ってボタンを作成する必要があると思いますが、ターゲット/アクションを設定する必要はありません。テーブルビューの詳細開示とは異なる設定になっています。試してみてください。 –

+0

ボタンを作成し、それに吹き出しを設定するだけで動作します。ありがとうございました。 – Seb

0

を高く評価しています。上記の方法の間に、代わりにMKPinAnnotationViewのこの新しいオブジェクトを作成し、渡された注釈にid<MKAnnotation>プロパティを設定します。

+0

送信者がMKPinAnnotationViewではなくボタンであることを示す場合は、どのように役立ちますか? – Seb

関連する問題