0

この関数を使用して、2 点間のパスを描画しました。Google の API から座標を取得しますが、これで問題はありません。私の問題は、特定のポイントが描画したパス内にあるかどうかを指定するにはどうすればよいですか? を使用しようとしていますCGContextPathContainsPoint()が、運がありません。

-(void)updateRouteView {
    CGContextRef context =  CGBitmapContextCreate(nil,
                                                 routeView.frame.size.width,
                                                 routeView.frame.size.height,
                                                 8,
                                                 4 * routeView.frame.size.width,
                                                 CGColorSpaceCreateDeviceRGB(),
                                                 kCGImageAlphaPremultipliedLast);

    CGContextSetStrokeColorWithColor(context, lineColor.CGColor);
    CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);
    CGContextSetLineWidth(context, 5.0);

    // routes : array of coordinates of the path
    for (int i = 0; i < routes.count; i++) {
        CLLocation* location = [routes objectAtIndex:i];
        CGPoint point = [mapView convertCoordinate:location.coordinate toPointToView:routeView];

        if (i == 0)
            CGContextMoveToPoint(context, point.x, routeView.frame.size.height - point.y);
        else
            CGContextAddLineToPoint(context, point.x, routeView.frame.size.height - point.y);
    }

    CGContextStrokePath(context);

    // this is the point I want to check if it is inside the path
    CLLocationCoordinate2D checkedLocation;
    checkedLocation.latitude = 37.782756;
    checkedLocation.longitude =  -122.409647;
    CGPoint checkedPoint = [mapView convertCoordinate:checkedLocation toPointToView:routeView];
    checkedPoint.y = routeView.frame.size.height - checkedPoint.y;

    // even if the checkedPoint is inside the path, the result is false    
    BOOL checking = CGContextPathContainsPoint(context, checkedPoint, kCGPathStroke);
    if (checking)
        NSLog(@"YES");
    else
        NSLog(@"NO");

    CGImageRef image = CGBitmapContextCreateImage(context);
    UIImage* img = [UIImage imageWithCGImage:image];

    routeView.image = img;
    CGContextRelease(context);
}
4

1 に答える 1

3

ポイントを確認した後、 CGContextStrokePath(context) を呼び出します。クォーツは、打たれると現在のパスをクリアします。

于 2012-04-20T18:42:19.197 に答える