MKMapView,触摸pinview时如何添加自定义视图?

发布于 2024-11-02 01:27:50 字数 272 浏览 1 评论 0原文

MKMapView 默认情况下,点击 pin 视图会出现带有标题和副标题的小黑框,但现在我想在视图上显示更多的数据。我想在单击图钉视图时显示客户视图。

有人做过类似的事情吗?我尝试过以下方法,但都不起作用:

  1. 使用代理访问mapView的点击事件:didSelectAnnotationView,但以上委托只支持ios4.x
  2. 使用tapgesture捕获事件,仅ios3.2之后支持
  3. 尝试继承MKMapView,发现无法获取触摸事件。

MKMapView default, click on the pin view will appear with the title and subtitle of a small black box, but now I want to display the data more on a view. I want to display a customer view when I click on a pin view.

Has anyone done something similar? I have tried the following methods, but they did not work:

  1. using a proxy access to the click event mapView: didSelectAnnotationView, but more than the delegate only supports ios4.x
  2. use tapgesture to capture the event, only support after ios3.2
  3. try to inherit MKMapView, found it impossible to get touch events.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云醉月微眠 2024-11-09 01:27:50

也可能重复 带有自定义 MKAnnotation 的 MKMapView
以下是创建自定义标注的方法
http://blog.asolutions.com /2010/09/building-custom-map-annotation-callouts-part-1/

想知道如何解决
“3.尝试继承MKMapView,发现无法获取触摸事件。”在早于iOS4.x的设备上,这里是如何

  • 子类MKAnnotationView,如下所示(.h和.m文件)

.h文件

    #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

.m文件

#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
  • 然后在mapView的UIViewController中你需要设置ClickableAnnotation委托和选择器

-(MKAnnotationView *)mapView :(MKMapView )mapViewParam viewForAnnotation:(id )annotation {
静态 NSString
PIN_RECYCLE_ID = @"pin";

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

}

also possible duplicate of MKMapView with custom MKAnnotation
Here is how you can create a custom callout
http://blog.asolutions.com/2010/09/building-custom-map-annotation-callouts-part-1/

Wondering how to solve
"3. try to inherit MKMapView, found it impossible to get touch events." on earlier than iOS4.x devices, here is how

  • Subclass MKAnnotationView, shown below ( .h and .m files )

.h file

    #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

.m file

#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
  • Then in UIViewController for mapView you need to set the ClickableAnnotation delegate and selector

-(MKAnnotationView *)mapView:(MKMapView )mapViewParam viewForAnnotation:(id )annotation {
static NSString
PIN_RECYCLE_ID = @"pin";

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文