Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing MKMapView Overlay like Google Maps Directions

iOS 5 changed the way the built-in Google Maps App draws routes:

Apple's line

I would now like to replicate the design of the route overlay in my own app but I am currently only able to draw a plain blue line. I would like to add the 3D-effect with the gradient, borders and the glow. Any ideas on how to accomplish this?

Currently I'm using the following code:

CGContextSetFillColorWithColor(context, fillColor.CGColor);
CGContextSetLineJoin(context, kCGLineJoinRound);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, lineWidth);
CGContextAddPath(context, path);
CGContextReplacePathWithStrokedPath(context);
CGContextFillPath(context);

Resulting in a rather ugly line:

my line

Thanks!

Update: The solution should work on iOS 4.0 and up.

like image 517
myell0w Avatar asked Dec 30 '11 00:12

myell0w


People also ask

How to Draw Route on MapKit iOS Swift?

swift file is visible. Ctrl and drag from the Map Kit View to the ViewController class to create the following Outlet. Select the Map Kit View and select the Pin icon in the lower-right corner of the Interface Builder (3rd button from the left). Select all pin directions and deselect "Constrain to margins".

What is MapKit in iOS?

MapKit is a powerful API available on iOS devices that makes it easy to display maps, mark locations, enhance with custom data and even draw routes or other shapes on top.


2 Answers

I would say they are drawing a CGPath around the original line, stroking the edges and gradient filling it. The ends are capped by adding a semi circle to the CGPath.

Would be a bit more work than simply drawing a single line and stroking it, but gives them full control over the style of the rendered path.

like image 38
Chris Miles Avatar answered Sep 18 '22 07:09

Chris Miles


I think that @ChrisMiles is correct in that the segments are probably being drawn individually. (I initially thought that this might have been doable using CGPatternRef but you don't have any access to the CTM or path endpoints inside the pattern drawing callback.)

With this in mind, here is an exceedingly crude, back-of-the-envelope example of how you might begin such an effort (filling the segments individually). Note that:

  • gradient colors are guessed
  • end caps are nonexistent and will need to be separately implemented
  • some aliasing artifacts remain
  • not a great deal of attention was paid to performance

Hopefully this can get you started at least (and works through some of the analytic geometry).

-  (CGGradientRef)lineGradient
{
    static CGGradientRef gradient = NULL;
    if (gradient == NULL) {
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        CGColorRef white = [[UIColor colorWithWhite:1.f
                                              alpha:0.7f] CGColor];
        CGColorRef blue = [[UIColor colorWithRed:0.1f
                                           green:0.2f
                                            blue:1.f
                                           alpha:0.7f] CGColor];
        CGColorRef lightBlue = [[UIColor colorWithRed:0.4f
                                                green:0.6f
                                                 blue:1.f
                                                alpha:0.7f] CGColor];
        CFMutableArrayRef colors = CFArrayCreateMutable(kCFAllocatorDefault, 
                                                        8,
                                                        NULL);
        CFArrayAppendValue(colors, blue);
        CFArrayAppendValue(colors, blue);
        CFArrayAppendValue(colors, white);
        CFArrayAppendValue(colors, white);
        CFArrayAppendValue(colors, lightBlue);
        CFArrayAppendValue(colors, lightBlue);
        CFArrayAppendValue(colors, blue);
        CFArrayAppendValue(colors, blue);
        CGFloat locations[8] = {0.f, 0.08f, 0.14f, 0.21f, 0.29f, 0.86f, 0.93f, 1.f};
        gradient = CGGradientCreateWithColors(colorSpace,
                                              colors,
                                              locations);
        CFRelease(colors);
        CGColorSpaceRelease(colorSpace);
    }
    return gradient;
}

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);

    CGContextSetAllowsAntialiasing(context, YES);
    CGContextSetShouldAntialias(context, YES);

    // Fill background color
    [[UIColor whiteColor] setFill];
    UIRectFill(rect);

    // Build a path
    CGFloat strokeWidth = 10.f;
    CGContextSetLineWidth(context, strokeWidth);

    CGGradientRef gradient = [self lineGradient];

    CGPoint points[9] = {
        CGPointMake(10.f, 25.f),
        CGPointMake(100.f, 100.f),
        CGPointMake(100.f, 150.f),
        CGPointMake(22.f, 300.f),
        CGPointMake(230.f, 400.f),
        CGPointMake(230.f, 200.f),
        CGPointMake(300.f, 200.f),
        CGPointMake(310.f, 160.f),
        CGPointMake(280.f, 100.f)
    };


    for (NSUInteger i = 1; i < 9; i++) {
        CGPoint start = points[i - 1];
        CGPoint end = points[i];
        CGFloat dy = end.y - start.y;
        CGFloat dx = end.x - start.x;
        CGFloat xOffset, yOffset;
        // Remember that, unlike Cartesian geometry, origin is in *upper* left!
        if (dx == 0) {
            // Vertical to start, gradient is horizontal
            xOffset = 0.5 * strokeWidth;
            yOffset = 0.f;
            if (dy < 0) {
                xOffset *= -1;
            }
        }
        else if (dy == 0) {
            // Horizontal to start, gradient is vertical
            xOffset = 0.f;
            yOffset = 0.5 * strokeWidth;
        }
        else {
            // Sloped
            CGFloat gradientSlope = - dx / dy;
            xOffset = 0.5 * strokeWidth / sqrt(1 + gradientSlope * gradientSlope);
            yOffset = 0.5 * strokeWidth / sqrt(1 + 1 / (gradientSlope * gradientSlope));
            if (dx < 0 && dy > 0) {
                yOffset *= -1;
            }
            else if (dx > 0 && dy < 0) {
                xOffset *= -1;
            }
            else if (dx < 0 && dy < 0) {
                yOffset *= -1;
                xOffset *= -1;
            }
            else {
            }
        }
        CGAffineTransform startTransform = CGAffineTransformMakeTranslation(-xOffset, yOffset);
        CGAffineTransform endTransform = CGAffineTransformMakeTranslation(xOffset, -yOffset);
        CGPoint gradientStart = CGPointApplyAffineTransform(start, startTransform);
        CGPoint gradientEnd = CGPointApplyAffineTransform(start, endTransform);

        CGContextSaveGState(context);
        CGContextMoveToPoint(context, start.x, start.y);
        CGContextAddLineToPoint(context, end.x, end.y);
        CGContextReplacePathWithStrokedPath(context);
        CGContextClip(context);
        CGContextDrawLinearGradient(context, 
                                    gradient, 
                                    gradientStart, 
                                    gradientEnd, 
                                    kCGGradientDrawsAfterEndLocation | kCGGradientDrawsBeforeStartLocation);
        CGContextRestoreGState(context);
    }

    CGContextRestoreGState(context);
}
like image 176
Conrad Shultz Avatar answered Sep 21 '22 07:09

Conrad Shultz