2012-04-20 6 views
0

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

あなたは 'CGColorSpaceCreateDeviceRGB()'によって作成された 'CGColorSpace'値を漏らしています。 –

答えて

3

あなたのポイントをチェックした後で、CGContextStrokePath(context)を呼び出してください。クォーツは、一度打たれると現在のパスをクリアします。

+0

多くのあなたに感謝!何がシンプルなものxD、私はあなたの答え+1したいが、私は十分な評判を持っていない – mbrmj

+0

それは問題ではない。喜んで助ける – mprivat

関連する問題