0

我用这个函数在两点之间画了一条路径。我从谷歌的 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) 。一旦被击中,Quartz 将清除当前路径。

于 2012-04-20T18:42:19.197 回答