0

この質問に対する多くの回答を検索しましたが、どれも私が望んでいることを正確に実行していないようです。多くのチュートリアルでは、コードで線とポリゴンを追加する方法を教えてくれますが、フリーハンドでの描画ではありません。

マップ上に UIImageView を持つ MKMapView を取得しました。そして、UIImageView に少し描画できますが、その直後に MKMapView がドラッグされ、描画が続行されません。

私は何を間違えたのか知りたいですか?

タッチイベントのコードは次のとおりです。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    pointA = [touch locationInView:drawView];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch *touch = [touches anyObject];
        CGPoint currentLocation = [touch locationInView:drawView];

        UIGraphicsBeginImageContext(drawView.frame.size);
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        [drawView.image drawInRect:CGRectMake(0, 0, drawView.frame.size.width, drawView.frame.size.height)];
        CGContextSetLineCap(ctx, kCGLineCapRound);
        CGContextSetLineWidth(ctx, 5.0);
        CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);
        CGContextBeginPath(ctx);
        CGContextMoveToPoint(ctx, pointA.x, pointA.y);
        CGContextAddLineToPoint(ctx, currentLocation.x, currentLocation.y);
        CGContextStrokePath(ctx);
        drawView.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        pointA = currentLocation;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch *touch = [touches anyObject];
        CGPoint currentLocation = [touch locationInView:drawView];

        UIGraphicsBeginImageContext(drawView.frame.size);
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        [drawView.image drawInRect:CGRectMake(0, 0, drawView.frame.size.width, drawView.frame.size.height)];
        CGContextSetLineCap(ctx, kCGLineCapRound);
        CGContextSetLineWidth(ctx, 5.0);
        CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);
        CGContextBeginPath(ctx);
        CGContextMoveToPoint(ctx, pointA.x, pointA.y);
        CGContextAddLineToPoint(ctx, currentLocation.x, currentLocation.y);
        CGContextStrokePath(ctx);
        drawView.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        pointA = currentLocation;
} 

UIImageView を表示するボタンのコード:

編集:マップとの相互作用を停止するためにこれを作成しました:

- (IBAction)modeDraw:(id)sender {
    if (drawView.alpha == 0.0) {
        drawView.image=nil; //empty for a new draw
        drawView.backgroundColor = [UIColor whiteColor];
        [UIImageView beginAnimations:nil context:nil];
        [UIImageView setAnimationDuration:0.5];
        [drawView setAlpha:0.4];
        [UIImageView commitAnimations];
        myMapView.zoomEnabled = NO;
        myMapView.scrollEnabled = NO;
    } else {
        [UIImageView beginAnimations:nil context:nil];
        [UIImageView setAnimationDuration:0.5];
        [drawView setAlpha:0.0];
        [UIImageView commitAnimations];
        myMapView.zoomEnabled = YES;
        myMapView.scrollEnabled = YES;
    }
}

描画を行った後、この描画をフォーム(エリア)に変換してマップに配置したいと思います。私にいくつかの兆候があれば?

ありがとう、私の悪い英語でごめんなさい。

4

2 に答える 2

0

誰かが私と同じ問題を抱えている場合のコードと、アランがくれたリンクは次のとおりです。http://www.apps168.net/post/1460.html :

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    pointA = [touch locationInView:drawView];
    pathRef = CGPathCreateMutable();
    CGPathMoveToPoint(pathRef, NULL, pointA.x, pointA.y);
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint currentLocation = [touch locationInView:drawView];
    CGPoint pastLocation = [touch previousLocationInView:drawView];

    UIGraphicsBeginImageContext(drawView.frame.size);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [drawView.image drawInRect:CGRectMake(0, 0, drawView.frame.size.width, drawView.frame.size.height)];
    CGContextSetLineCap(ctx, kCGLineCapRound);
    CGContextSetLineWidth(ctx, 5.0);
    CGContextSetRGBStrokeColor(ctx, 0.0, 0.0, 0.0, 1.0);
    CGContextBeginPath(ctx);
    CGContextStrokePath(ctx);

    CGContextMoveToPoint(ctx, pastLocation.x, pastLocation.y);
    CGContextAddLineToPoint(ctx, currentLocation.x, currentLocation.y);
    CGContextDrawPath(ctx, kCGPathFillStroke);
    drawView.image=UIGraphicsGetImageFromCurrentImageContext();

    CGPathAddLineToPoint(pathRef, NULL, currentLocation.x, currentLocation.y);
    UIGraphicsEndImageContext();

    //pointA = currentLocation;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    CGPathCloseSubpath(pathRef);

    NSUInteger count = [array count];
    for (NSUInteger i = 0; i < count; i=i+2) {
        NSString *lat = [array objectAtIndex: i];
        NSString *lgt = [array objectAtIndex: i+1];

        CLLocationCoordinate2D pointLoc;
        pointLoc.latitude = [lat doubleValue];
        pointLoc.longitude = [lgt doubleValue];

        locationConverToImage = [myMapView convertCoordinate:pointLoc toPointToView:drawView];
        if (CGPathContainsPoint(pathRef, NULL, locationConverToImage, NO)) {
            NSLog(@"point in path!");
            //sélection de ces points et cacher les autres.
        }
    }

    [UIImageView beginAnimations:nil context:nil];
    [UIImageView setAnimationDuration:0.5];
    [drawView setAlpha:0.0];
    [UIImageView commitAnimations];
    myMapView.zoomEnabled = YES;
    myMapView.scrollEnabled = YES;

}
于 2013-04-19T10:59:25.287 に答える
0

Draw my mapこれを試してみてください、これはドローマップポイントプロジェクトです

于 2014-11-14T09:48:11.417 に答える