开发者

Resizing the Lines Drawing on Map on Zoom in and Zoom Out

I am drawing some custom lines using custom MKAnnotationView on MapView control. The drawing is between two defined points. The problem I am facing is that, when I zoom in or zoom out in an iPhone, the lines seems to zoom in and out too. This creates a weird effect as lines appear to be over different buildings and places once the user zoom in or out. How can I solve this problem?

Here is the code for custom AnnotationView:

@implementation JogAnnotationView

@synthesize points; 

- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier pts:(NSMutableArray *)p
{
    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
    if (self != nil)
    {
        CGRect cgRect =[[UIScreen mainScreen] bounds];
        CGSize cgSize = cgRect.size;

        CGRect frame = self.frame;
        frame.size = CGSizeMake(cgSize.width/2, cgSize.height/2);
        self.frame = frame;
        self.backgroundColor = [UIColor clearColor];
        self.centerOffset = CGPointMake(-5, -5);
        self.points = p;
    }

    return self;
}

- (void)drawRect:(CGRect)rect
{
    NSLog(@"%d",self.points.count);

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextBeginPath (context);
    CGContextSetLineWidth(context, 2.0);
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
    CGFloat components[] = {0.0, 0.0, 1.0, 1.0};

    CGColorRef color = CGColorCreate(colorspace, components);

    CGContextSetStrokeColorWithColor(context, color);
    CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);

    for (int i = 0; i < ([self.points count] - 1); i++)
    {
        JogPoint *startPoint = (JogPoint *) [self.points objectAtIndex:i];
        JogPoint *endPoint = (JogPoint *) [self.points objectAtIndex:(i+1)];

        CGContextMoveToPoint(context, startPoint.x ,startPoint.y);
        CGContextAddLineToPoint(context,endPoint.x,endPoint.开发者_StackOverflowy); 
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜