Skip to content

Instantly share code, notes, and snippets.

@ajmath
Created September 30, 2010 18:39
Show Gist options
  • Save ajmath/605077 to your computer and use it in GitHub Desktop.
Save ajmath/605077 to your computer and use it in GitHub Desktop.
//Overridden MKOverlayView method that draws the overlay to the MapView
- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context
{
//Create CGPath to be rendered
CGMutablePathRef cgPath = CGPathCreateMutable();
CGPoint p1, p2, p3;
p1.x = -40;
p1.y = 20;
p2.x = 0.0;
p2.y = -20;
p3.x = 40;
p3.y = 20;
CGPathMoveToPoint(cgPath, NULL, p1.x, p1.y);
CGPathAddLineToPoint(cgPath, NULL, p2.x, p2.y);
CGPathMoveToPoint(cgPath, NULL, p2.x, p2.y);
CGPathAddLineToPoint(cgPath, NULL, p3.x, p3.y);
CGPathMoveToPoint(cgPath, NULL, p3.x, p3.y);
CGPathAddLineToPoint(cgPath, NULL, p1.x, p1.y);
CGPathCloseSubpath(cgPath);
//Get a CGPoint that the path will be centered on
MKMapPoint mp = MKMapPointForCoordinate(point.coordinate);
CGPoint origin = [self pointForMapPoint:mapRect.origin];
//Translate and add the path to the current CGContext
CGContextSaveGState(context);
CGContextTranslateCTM(context, origin.x, origin.y);
CGContextAddPath(context, cgPath);
CGContextRestoreGState(context);
CGFloat lineWidth = MKRoadWidthAtZoomScale(zoomScale) / 2.0f;
//Set CG variables and stroke/fill the path
CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 0.5);
CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 0.5);
CGContextSetLineJoin(context, kCGLineJoinBevel);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, lineWidth);
CGContextStrokePath(context);
CGContextFillPath(context);
CGPathRelease(cgPath);
}
@ajmath
Copy link
Author

ajmath commented Oct 6, 2010

@superaly Thanks for the comment!

Thats weird that it doesn't work with TranslateCTM.

I actually ended up combining CGContextStrokePath and CGContextFillPath with CGContextDrawPath and it worked. I still don't see why you cant use them both separately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment