MKMapView MKPointAnnotation点击事件 [英] MKMapView MKPointAnnotation tap event

查看:181
本文介绍了MKMapView MKPointAnnotation点击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个注释列表(MKPointAnnotation)。我有一个UIViewController用于整个视图,MKMapView实现Controller,我认为它对于检测用户与地图的交互非常有用,我自己的MKPointAnnotation实现(子类)Controller,它告诉我们如何显示注释。

I have a list of annotations (MKPointAnnotation). I have a UIViewController which is for the whole view, MKMapView implementing Controller, which I thought is useful for detecting the users interaction with the map, my own MKPointAnnotation implementing (subclass) Controller which tells how to show the annotation.

然而,我对用户检测到点击事件感到震惊。

However I'm struck at the detection of the tap event by the user.

谷歌搜索告诉我,我必须做点什么通过实现以下函数。

Googling told me that I have to do something by implementing the following function.

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

并且我必须在实现MapViewDelegate的某个类中实现它(协议)。

and also that I have to implement this in some class which implements the MapViewDelegate (Protocol).

但我很困惑,无法前进。谁能告诉我在哪里做什么?

But I'm all confused and unable to move forward. Can anyone tell me where to do what?

抱歉所有的大惊小怪!

推荐答案

有两种方法可以检测用户与注释视图的交互。常见的技巧是为您的 MKAnnotationView 定义一个标注(您在点击典型地图应用中的图钉时看到的标准小弹出气泡)。并在标准 viewForAnnotation 方法中为注释创建注释视图:

There are two ways of detecting user interaction with your annotation view. The common technique is to define a callout (that standard little popover bubble that you see when you tap on a pin in a typical maps app) for your MKAnnotationView. And you create the annotation view for your annotation in the standard viewForAnnotation method:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    MKAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"loc"];
    annotationView.canShowCallout = YES;
    annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

    return annotationView;
}

通过这样做,你会得到一个标注,但你正在添加一个权利附件,在我上面的例子中,是一个披露指示器。这样,他们点击您的注释视图(在上面的示例中,地图上的图钉),他们看到标注,当他们点击该标注的正确附件(此示例中的小披露指示符)时,您的 calloutAccessoryControlTapped 被调用(在下面的示例中,执行segue到某个详细视图控制器):

By doing this, you get a callout, but you're adding an right accessory, which is, in my example above, a disclosure indicator. That way, they tap on your annotation view (in my example above, a pin on the map), they see the callout, and when they tap on that callout's right accessory (the little disclosure indicator in this example), your calloutAccessoryControlTapped is called (in my example below, performing a segue to some detail view controller):

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    [self performSegueWithIdentifier:@"DetailsIphone" sender:view];
}

这是小型iPhone屏幕上非常典型的用户体验。

That's a very typical user experience on the small iPhone screen.

但是,如果你不喜欢那个用户体验并且你不想要标准的标注,而是你想要别的东西发生,你可以定义你的 MKAnnotationView 以便不显示标注,而是截取它并执行其他操作(例如,在iPad上映射应用程序时,您可能会显示一些更复杂的弹出框而不是标准标注)。例如,您可以让 MKAnnotationView 不显示标注:

But, if you don't like that UX and you don't want the standard callout, but rather you want something else to happen, you can define your MKAnnotationView so that a callout is not shown, but instead you intercept it and do something else (for example, on iPad maps apps, you might show some more sophisticated popover rather than the standard callout). For example, you could have your MKAnnotationView not show a callout:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    MKAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"loc"];
    annotationView.canShowCallout = NO;

    return annotationView;
}

但是你可以手动处理 didSelectAnnotationView 检测用户何时点击 MKAnnotationView ,在此示例中显示一个popover:

But you can then manually handle didSelectAnnotationView to detect when a user tapped on your MKAnnotationView, in this example showing a popover:

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
    [mapView deselectAnnotation:view.annotation animated:YES];

    DetailsViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailsPopover"];
    controller.annotation = view.annotation;
    self.popover = [[UIPopoverController alloc] initWithContentViewController:controller];
    self.popover.delegate = self;
    [self.popover presentPopoverFromRect:view.frame
                                  inView:view.superview
                permittedArrowDirections:UIPopoverArrowDirectionAny
                                animated:YES];
}

我在上面的代码中为用户界面添加了一些屏幕快照< a href =https://stackoverflow.com/a/14619356/1271826>我的回答。

I include some screen snapshots for the user interface yielded by the above code in my answer here.

这篇关于MKMapView MKPointAnnotation点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆