在MKMapView的叠加层上触摸事件

Ben*_*Ben 9 overlay mkmapview touch-event mkpolyline

在我正在设计的应用程序中,我有一个MKMapView覆盖它(自定义MKPolylinesbtw),我希望能够检测这些覆盖上的触摸事件,并为每个覆盖分配特定的操作.有人可以帮我这个吗?谢谢 !

贝尼亚

Jan*_*ano 18

这可以通过组合如何拦截MKMapView或UIWebView对象上的触摸事件来解决以及如何确定注释是否在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];
Run Code Online (Sandbox Code Playgroud)

WildcardGestureRecognizer是第一个相关的答案.调用mapView:viewForOverlay:并不便宜,添加这些的本地缓存会有所帮助.

  • 将MKPolygonView替换为MKPolygonRenderer - 前者在iOS7中已弃用 (5认同)

Ben*_*Ben 0

以防万一它可能对你们中的一些人有帮助......我找不到办法做到这一点,但我在叠加层上添加了注释(无论如何,我需要这样做来显示一些信息),然后我就可以触摸了该注释上的事件。我知道这不是最好的方法,但在我的情况下,也许在你的情况下,它有效;)!