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;
}

問題は、最初のログが呼び出される上記のコードのポイントです。クラスは常に を返すよう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);
4

2 に答える 2

3

これが必要だと思います:

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

でしたタップ:

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

マップ上のポイントの新しいリストを追加する前に。

于 2014-01-06T13:34:08.947 に答える
0

WildcardGestureRecognizer と UIGestureRecognizer を使用して動作させました。コードについては私の投稿を参照してください。

于 2014-01-06T17:50:08.543 に答える