mkmapview - MKMapView 覆盖层上的触摸事件

标签 mkmapview overlay touch-event mkpolyline

在我当前设计的应用程序中,我有一个带有覆盖层的 MKMapView (顺便说一句,自定义的 MKPolylines),我希望能够检测到触摸事件这些覆盖层并为每个覆盖层分配特定的操作。有人可以帮我解决这个问题吗? 谢谢!

本贾

最佳答案

这可以结合 How to intercept touches events on a MKMapView or UIWebView objects? 来解决和 How to determine if an annotation is inside of MKPolygonView (iOS) 。在 viewWillAppear 中添加以下内容:

WildcardGestureRecognizer * tapInterceptor = [[WildcardGestureRecognizer alloc] init];
tapInterceptor.touchesBeganCallback = ^(NSSet * touches, UIEvent * event) {
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self.mapView];

    CLLocationCoordinate2D coord = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];
    MKMapPoint mapPoint = MKMapPointForCoordinate(coord);
    for (id overlay in self.mapView.overlays) 
    {
        if ([overlay isKindOfClass:[MKPolygon class]])
        {
            MKPolygon *poly = (MKPolygon*) overlay;
            id view = [self.mapView viewForOverlay:poly];
            if ([view isKindOfClass:[MKPolygonView class]])
            {
                MKPolygonView *polyView = (MKPolygonView*) view;
                CGPoint polygonViewPoint = [polyView pointForMapPoint:mapPoint];
                BOOL mapCoordinateIsInPolygon = CGPathContainsPoint(polyView.path, NULL, polygonViewPoint, NO);   
                if (mapCoordinateIsInPolygon) {
                    debug(@"hit!") 
                } else {
                    debug(@"miss!");   
                }
            }
        }
    }

};
[self.mapView addGestureRecognizer:tapInterceptor];

WildcardGestureRecognizer 位于第一个链接的答案中。调用 mapView:viewForOverlay: 的成本并不便宜,添加这些内容的本地缓存会有所帮助。

关于mkmapview - MKMapView 覆盖层上的触摸事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6436479/

相关文章:

iphone - 如何获得 objective-c 中两个位置之间的距离(以英里为单位)?

Jquery 页面覆盖

ios - 如何在 ios 的 subview Controller 中检测触摸事件

iPhone:将 MKMapView 与另一个 UITapGestureRecognizer 相结合

iphone - 从 MKUserTrackingModeFollowWithHeading 切换模式时如何始终保持 iOS 6 中的 MKMapView 朝北

iphone - 在 iPhone 中使用 MapKit 显示带有字符串的地址

java - 如何在模态窗口上方显示工具提示内容?

html - 嵌入式 Vimeo 视频上的 CSS 覆盖不适用于 Firefox 全屏

android - 检测触摸坐标是否在自定义 View 上可点击区域的坐标内

iOS:ccTouchesBegan 可以包含多个触摸吗?