首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MKMapView MKPointAnnotation点击事件

MKMapView MKPointAnnotation点击事件
EN

Stack Overflow用户
提问于 2013-03-08 18:54:11
回答 3查看 72.4K关注 0票数 46

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

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

Googling告诉我,我必须通过实现以下函数来做一些事情。

代码语言:javascript
复制
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control

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

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

很抱歉这么大惊小怪!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-03-08 23:50:13

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

代码语言:javascript
复制
- (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被调用(在我下面的例子中,执行到一些细节视图控制器的分段):

代码语言:javascript
复制
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    [self performSegueWithIdentifier:@"DetailsIphone" sender:view];
}

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

但是,如果您不喜欢该UX并且不想要标准的标注,而是希望发生其他事情,则可以定义MKAnnotationView,以便不显示标注,而是截取它并执行其他操作(例如,在iPad地图应用程序中,您可能会显示一些更复杂的弹出窗口,而不是标准的标注)。例如,您可以让MKAnnotationView不显示标注:

代码语言:javascript
复制
- (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,在此示例中显示了一个弹出式窗口:

代码语言:javascript
复制
- (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];
}

我在my answer here中包含了由上面的代码生成的用户界面的一些屏幕快照。

票数 106
EN

Stack Overflow用户

发布于 2016-11-30 18:53:39

Swift 3的Robs示例:

代码语言:javascript
复制
class MapViewController: UIViewController
{
    override func viewDidLoad()
    {
        super.viewDidLoad()

        // Create map view in storyboard
        view.delegate = self
    }
}

extension MapViewController: MKMapViewDelegate
{
    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?
    {
        let annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "annotationView")
        annotationView.canShowCallout = true
        annotationView.rightCalloutAccessoryView = UIButton.init(type: UIButtonType.detailDisclosure)

        return annotationView
    }

    func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl)
    {
        guard let annotation = view.annotation else
        {
            return
        }

        let urlString = "http://maps.apple.com/?sll=\(annotation.coordinate.latitude),\(annotation.coordinate.longitude)"
        guard let url = URL(string: urlString) else
        {
            return
        }

        UIApplication.shared.openURL(url)
    }
}
票数 9
EN

Stack Overflow用户

发布于 2013-03-08 19:21:18

此方法中的Add按钮

代码语言:javascript
复制
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 
{
  pinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
  pinView.canShowCallout = YES;
}

Then callout method
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
      InfoView *infoView = [[InfoView alloc]initWithNibName:@"InfoView" bundle:nil];
        [self.navigationController pushViewController:infoView animated:YES];

} 
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15292318

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档