在MKMapView上绘制UIImageView

时间:2013-04-18 13:13:35

标签: ios objective-c ipad touch mkmapview

我已经搜索了很多关于这个问题的答案,但是他们似乎都没有完全符合我的要求。很多教程向我展示了如何在代码中添加线条和多边形,而不是徒手绘制。

我在地图上有一个带有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;
    }
}

完成绘制后,我想将此绘制转换为一个表单(区域),将其放在地图上。如果你有一些迹象表明我?

谢谢,抱歉我的英语不好。

2 个答案:

答案 0 :(得分:0)

以下是我的代码,如果有人遇到与我相同的问题以及Allan给我的链接: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;

}

答案 1 :(得分:0)

Draw my map试试这个,这是绘制地图点项目