检测MKPolyline上的触摸手势

5

我的应用程序中有一个带有几个MKGeodesicPolylines的MapView地图视图。我想要能够识别这些线上的触摸手势。

使用以下方法添加覆盖层:

[_mapView addOverlay:polyline];

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id < MKOverlay >)overlay
{
    if ([overlay class] == [MKGeodesicPolyline class])
    {
        MKPolylineRenderer *renderer = [[[MKPolylineRenderer alloc] initWithPolyline:overlay] autorelease];
        renderer.lineWidth = 4.0;
        renderer.strokeColor = [UIColor blackColor];
        return renderer;
    }
 return nil;
}

我尝试了WildcardGestureRecognizer,应该符合我的目的。这是我正在使用的代码:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {

        .... MapView init ....

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

            CLLocationCoordinate2D coord = [_mapView convertPoint:point toCoordinateFromView:self.mapView];
            MKMapPoint mapPoint = MKMapPointForCoordinate(coord);
            for (id overlay in _mapView.overlays)
            {
                if ([overlay isKindOfClass:[MKGeodesicPolyline class]])
                {
                    MKGeodesicPolyline *poly = (MKGeodesicPolyline*) overlay;
                    id view = [_mapView viewForOverlay:poly];

                    NSLog(@"view class: %@",[view class]);

                    if ([view isKindOfClass:[MKPolylineRenderer class]])
                    {
                        MKPolylineRenderer *polyView = (MKPolylineRenderer*) view;
                        CGPoint polygonViewPoint = [polyView pointForMapPoint:mapPoint];
                        BOOL mapCoordinateIsInPolygon = CGPathContainsPoint(polyView.path, NULL, polygonViewPoint, NO);
                        if (mapCoordinateIsInPolygon) {
                            NSLog(@"hit!");
                        } else {
                            NSLog(@"miss!");
                        }
                    }

                }
            }

        };
        [_mapView addGestureRecognizer:tapInterceptor];
    }
    return self;
}

问题在上面代码中第一个Log被调用的地方。该类似乎总是返回null。日志输出为: 2014-01-06 13:50:41.106 App[11826:60b] view class: (null) 我希望有人能指点我正确的方向。先感谢您!
有效代码:
我已经让它对我起作用了。希望能帮到别人:
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {

        .... MapView init ....

        WildcardGestureRecognizer * tapInterceptor = [[WildcardGestureRecognizer alloc] init];
        tapInterceptor.touchesBeganCallback = ^(NSSet * touches, UIEvent * event) {

            CGPoint point = [tapInterceptor locationInView:_mapView];

            CLLocationCoordinate2D coord = [_mapView convertPoint:point toCoordinateFromView:self.mapView];
            MKMapPoint mapPoint = MKMapPointForCoordinate(coord);
            for (id overlay in _mapView.overlays)
            {
                if ([overlay isKindOfClass:[MKGeodesicPolyline class]])
                {
                    MKGeodesicPolyline *poly = (MKGeodesicPolyline*) overlay;
                    id view = [_mapView viewForOverlay:poly];

                    NSLog(@"view class: %@",[view class]);

                    if ([view isKindOfClass:[MKPolylineRenderer class]])
                    {
                        MKPolylineRenderer *polyView = (MKPolylineRenderer*) view;
                        [polyView invalidatePath];

                        CGPoint polygonViewPoint = [polyView pointForMapPoint:mapPoint];

                        BOOL mapCoordinateIsInPolygon = CGPathContainsPoint(polyView.path, NULL, polygonViewPoint, NO);

                            if (mapCoordinateIsInPolygon)
                            {
                                NSLog(@"hit!");
                            } else {
                                NSLog(@"miss!");
                            }

                    }

                }
            }

        };
        [_mapView addGestureRecognizer:tapInterceptor];
    }
    return self;
}

替代方案:

添加UITapGestureRecognizer:

UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
[_mapView addGestureRecognizer:recognizer];
[recognizer release];

处理手势:

- (void)handleGesture:(UIGestureRecognizer *)recognizer
{

if (recognizer.state == UIGestureRecognizerStateEnded)
{
    for (int i=0; i<recognizer.numberOfTouches; i++)
    {
        //CGPoint point = [recognizer locationInView:_mapView];
        CGPoint point = [recognizer locationOfTouch:i inView:_mapView];

        CLLocationCoordinate2D coord = [_mapView convertPoint:point toCoordinateFromView:self.mapView];
        MKMapPoint mapPoint = MKMapPointForCoordinate(coord);

        for (id overlay in _mapView.overlays)
        {
            if ([overlay isKindOfClass:[MKGeodesicPolyline class]])
            {
                MKGeodesicPolyline *poly = (MKGeodesicPolyline*) overlay;
                id view = [_mapView rendererForOverlay:poly];

                if ([view isKindOfClass:[MKPolylineRenderer class]] && [[poly title] isEqualToString:@"fullRouteAbove"])
                {

                    MKPolylineRenderer *polyView = (MKPolylineRenderer*) view;
                    [polyView invalidatePath];

                    CGPoint polygonViewPoint = [polyView pointForMapPoint:mapPoint];
                    NSLog(@"polyView: %@",polyView);
                    BOOL mapCoordinateIsInPolygon = CGPathContainsPoint(polyView.path, NULL, polygonViewPoint, NO);

                    if (mapCoordinateIsInPolygon)
                    {
                        NSLog(@"hit!");
                    }else{
                        NSLog(@"miss!");
                    )

                }

            }
        }


    }

}
}

切换可触摸区域:

替换

BOOL mapCoordinateIsInPolygon = CGPathContainsPoint(polyView.path, NULL, polygonViewPoint, NO);

使用

BOOL mapCoordinateIsInPolygon = CGRectContainsPoint(CGPathGetBoundingBox(polyView.path), polygonViewPoint);

或者

BOOL mapCoordinateIsInPolygon = CGRectContainsPoint(CGPathGetPathBoundingBox(polyView.path), polygonViewPoint);

可能是因为代码调用了 viewForOverlay 而不是 rendererForOverlay。您还可能遇到 这个问题 - user467105
2个回答

3
我想你需要这个:
在你的类中添加一个名为arrPolylineViews的NSMutableArray。
然后在你的mapView上添加一个tapGestureRecognizer:
UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] init];
gr.numberOfTapsRequired = 1;
gr.numberOfTouchesRequired = 1;
gr.delegate = self;
[gr addTarget:self action:@selector(didTap:)];
[myMapView addGestureRecognizer:gr];

In didTap:

- (void)didTap:(UITapGestureRecognizer *)gr
{
    if (gr.state == UIGestureRecognizerStateEnded)
    {
        // convert the touch point to a CLLocationCoordinate & geocode
        CGPoint touchPoint = [gr locationInView:myMapView];
        MKPolylineView *touchedPolyLineView = [self polylineTapped:touchPoint];
        if (touchedPolyLineView)
        {
            //touched a polyLineView
        }
    }
}

然后:

- (MKOverlayView*)mapView:(MKMapView*)theMapView viewForOverlay:(id <MKOverlay>)overlay
{ 
    if([overlay isKindOfClass:[MKPolyline class]]){
        //create your polyLineView
        [arrPolylineViews addObject:polyLineView];
    }
}

然后添加这个方法:
- (MKPolylineView *)polylineTapped:(CGPoint)point
{
    // Check if the overlay got tapped
    for (MKPolylineView *polyView in arrPolylineViews)
    {
        // Get view frame rect in the mapView's coordinate system
        CGRect viewFrameInMapView = [polyView.superview convertRect:polyView.frame toView:myMapView];

        // Check if the touch is within the view bounds
        if (CGRectContainsPoint(viewFrameInMapView, point))
        {
            return polyView;
        }
    }
    return nil;
}

不要忘记 -

[arrPolylineViews removeAllObjects];

在添加新的地图点列表之前。

1
问题在于我使用的是iOS 7和MKPolylineRenderer而不是MKPolylineViews。这使得convertRect: toView:无法使用。 - freshking

0
我使用WildcardGestureRecognizer和UIGestureRecognizer使其正常工作。请查看我的帖子以获取代码。

当然可以自己回答问题,但不要把答案放在问题中,然后再发布一个回答说“看我的问题”。哈哈。 - Rob

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接