ios - 在 MKPolyline 上检测触摸手势

标签 ios uigesturerecognizer mkpolyline

在我的应用程序中,我有一个带有一些 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 类:(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];

handle 手势:

- (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);

最佳答案

我想你需要这个:

在你的类中添加一个名为 arrPolylineViews 的 NSMutableArray。

然后将 tapGestureRecognizer 添加到您的 mapView:

UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] init];
gr.numberOfTapsRequired = 1;
gr.numberOfTouchesRequired = 1;
gr.delegate = self;
[gr addTarget:self action:@selector(didTap:)];
[myMapView addGestureRecognizer:gr];

在 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];

在 map 上添加新的点列表之前。

关于ios - 在 MKPolyline 上检测触摸手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20950607/

相关文章:

ios - 实例成员目标不能用于类型 "ViewController"SWIFT XCODE

ios - UITableView 在快速滚动期间卡住

iphone - 尝试设置手势识别时崩溃

ios - 在 XCode 中调试时扩展数组的值

ios - 在模拟器中尝试 iPad 快捷方式

iOS:点击识别器不一致

ios - 如何在 UIPageViewController 上禁用多点触控?

swift - MKPolyLine swift 4

ios - map View 中两个位置的 Mapkit 路线不适用于印度经纬度?

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