2013-04-18 2 views
0

Я много раз искал ответ на этот вопрос, но ни один из них, похоже, не делает именно то, что я хочу. Много учебников показывают мне, как добавлять линии и полигоны в код, но не с рисованием вручную.Нарисуйте UIImageView над MKMapView

У меня есть MKMapView с UIImageView над картой. И я могу немного нарисовать свой 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:

EDIT: Я сделал это для останавливая взаимодействия с картой:

- (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; 
    } 
} 

После этого мой дро, я хотел бы превратить эту ничью в форму (область), который будет помещен на карту. Если у вас есть какие-то признаки для меня?

Спасибо, и извините за мой плохой английский.

+0

Сохранить данный рисунок в виде изображение и использовать это изображение в качестве аннотации на карте. Если это работа :) – Mangesh

ответ

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; 

} 
0

Draw my map попробовать это, это нарисовать карту точки проекта

+0

@bluefeet попробуйте это – Allan

Смежные вопросы