MKMapView,触摸pinview时如何添加自定义视图?
MKMapView默认情况下,点击pin视图会出现一个带有标题和副标题的小黑框,但现在我想在视图上显示更多的数据。我想在单击pin视图时显示客户视图 有人做过类似的事情吗?我尝试过以下方法,但都不起作用:MKMapView,触摸pinview时如何添加自定义视图?,mkmapview,Mkmapview,MKMapView默认情况下,点击pin视图会出现一个带有标题和副标题的小黑框,但现在我想在视图上显示更多的数据。我想在单击pin视图时显示客户视图 有人做过类似的事情吗?我尝试过以下方法,但都不起作用: 使用对单击事件mapView:didSelectAnnotationView的代理访问,但该代理仅支持ios4.x 使用Tap手势捕获事件,仅支持ios3.2之后的操作 尝试继承MKMapView,发现无法获取触摸事件。 也可能是重复的 以下是如何创建自定义详图索引 想知道如何解决 “3.尝
也可能是重复的 以下是如何创建自定义详图索引 想知道如何解决 “3.尝试继承MKMapView,发现无法获取触摸事件。”在早于iOS4.x的设备上,下面介绍如何
- 子类MKAnnotationView,如下所示(.h和.m文件)
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface ClickableMapAnnotationView : MKAnnotationView {
SEL pinClicked;
id delegate;
}
@property(nonatomic, assign) SEL pinClicked;
@property(nonatomic, assign) id delegate;
@end
- 然后,在UIViewController for mapView中,需要设置ClickableAnnotation委托和选择器
#import "ClickableMapAnnotationView.h"
@implementation ClickableMapAnnotationView
@synthesize pinClicked;
@synthesize delegate;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if ( delegate != nil && [delegate respondsToSelector:pinClicked] )
[delegate performSelector:pinClicked withObject:self.annotation];
[super touchesBegan:touches withEvent:event];
}
@end
if ( annotation == self.mapView.userLocation) // this is my location pin, skip
return nil;
ClickableMapAnnotationView* pin = (ClickableMapAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier: PIN_RECYCLE_ID];
if ( pin == nil ) {
pin = [[[ClickableMapAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier: PIN_RECYCLE_ID] autorelease];
// wire, pin clicked functionality
pin.delegate = self;
pin.pinClicked = @selector(annotationViewClicked:);
-(void) annotationViewClicked:(id) sender {
// map pin has been clicked
}