[はしり書き]MKMapViewのアノテーションのピン画像を変更 2010/05/15
2010/05/15

はしり書きメモです。
iphoneです。MKMapViewのアノテーションのピンをオリジナル画像に変更してみます。
なんかこみいっててよく理解しきれていないのですが。

まずはインターフェイスから。デリゲートを使います。


@interface MapViewController : UIViewController<MKMapViewDelegate> {
MKMapView* mapView;
}

で、実装部分で、MKMapViewを生成するときに、デリゲートで自分自身を渡して、
// delegate
mapView.delegate = self;


で、addAnnotationするときに、実際のところviewForAnnotationがよばれてMKAnnotationViewが返される(返す)。
で、viewForAnnotationを実装してあげます。
ここで、いろいろ切り分けて画像を指定できる感じかな。

実装部分はここを参考
[iPhone] MapKit にアイコン画像でピンを立ててタップ可能にする | Sun Limited Mt.


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

MKAnnotationView *annotationView;
NSString* identifier = @"Pin";
annotationView = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if(nil == annotationView) {
annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier] autorelease];
}
annotationView.image = [UIImage imageNamed:@"my_pin.png"];
annotationView.canShowCallout = YES;
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
annotationView.annotation = annotation;

return annotationView;
}


うーん、いまいちこれでいいのか不安になってきた...

<ため息>
コードはなんとなくできたのだけども、問題は自分でつくった画像がマップとそぐわない。
デフォルトのピンの画像みるとしっかり影もハイライトもついている。。。。
</ため息>
参考
iPhone - Annotation image disappears on touch - Stack Overflow
iPhoneのGPSとMapKitを使った地図を連動させる方法 - iPhoneSDK他いろいろ 開発メモ
iPhone Mapkitの使い方

: