iphone quartz drawing 2 lines on top of each other causes worm effect

Posted by Leonard on Stack Overflow See other posts from Stack Overflow or by Leonard
Published on 2010-01-14T00:41:45Z Indexed on 2012/09/19 9:38 UTC
Read the original article Hit count: 279

Filed under:
|
|
|
|

I'm using Quartz-2D for iPhone to display a route on a map. The route is colored according to temperature. Because some streets are colored yellow, I am using a slightly thicker black line under the route line to create a border effect, so that yellow parts of the route are spottable on yellow streets. But, even if the black line is as thick as the route line, the whole route looks like a worm (very ugly). I tought this was because I was drawing lines from waypoint to waypoint, instead using the last waypoint as the next starting waypoint. That way if there is a couple of waypoints missing, the route will still have no cuts.

What do I need to do to display both lines without a worm effect?

-(void) drawRect:(CGRect) rect
{
CSRouteAnnotation* routeAnnotation = (CSRouteAnnotation*)self.routeView.annotation;

// only draw our lines if we're not int he moddie of a transition and we 
// acutally have some points to draw. 
if(!self.hidden && nil != routeAnnotation.points && routeAnnotation.points.count >   0)
{
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    Waypoint* fromWaypoint = [[Waypoint alloc] initWithDictionary:[routeAnnotation.points objectAtIndex:0]];
    Waypoint* toWaypoint;

    for(int idx = 1; idx < routeAnnotation.points.count; idx++)
    {
        toWaypoint = [[Waypoint alloc] initWithDictionary:[routeAnnotation.points objectAtIndex:idx]];


        CLLocation* fromLocation = [fromWaypoint getLocation];
        CGPoint fromPoint = [self.routeView.mapView convertCoordinate:fromLocation.coordinate toPointToView:self];

        CLLocation* toLocation = [toWaypoint getLocation];
        CGPoint toPoint = [self.routeView.mapView convertCoordinate:toLocation.coordinate toPointToView:self];

        routeAnnotation.lineColor = [fromWaypoint.weather getTemperatureColor];

        CGContextBeginPath(context);
        CGContextSetLineWidth(context, 3.0);
        CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
        CGContextMoveToPoint(context, fromPoint.x, fromPoint.y);
        CGContextAddLineToPoint(context, toPoint.x, toPoint.y);
        CGContextStrokePath(context);
        CGContextClosePath(context);

        CGContextBeginPath(context);
        CGContextSetLineWidth(context, 3.0);
        CGContextSetStrokeColorWithColor(context,    routeAnnotation.lineColor.CGColor);
        CGContextMoveToPoint(context, fromPoint.x, fromPoint.y);
        CGContextAddLineToPoint(context, toPoint.x, toPoint.y);
        CGContextStrokePath(context);
        CGContextClosePath(context);


        fromWaypoint = toWaypoint;
    }

    [fromWaypoint release];
    [toWaypoint release];       
}
}

Also, I get a

<Error>: CGContextClosePath: no current point.

error, which I think is bullshit.

Please hint me! :)


© Stack Overflow or respective owner

Related posts about iphone

Related posts about drawing