iOS 7 Mapkit: MKPolygonRenderer subclass - drawMapRect method fails to draw text

1k Views Asked by At

My requirement is simple - I have many polygon overlay views to be laid out across my map view. In each of them I want some text. I need to change the text as well as text formatting based on user's activity on particular MKPolygon view.

Since my requirement is with polygons, I decided to subclass MKPolygonRenderer because of following hierarchy:

MKPolygonRenderer : MKOverlayPathRenderer : MKOverlayRenderer

I decided to subclass MKPolygonRenderer because I need region borders that are irregularly shaped. Inside them, I need text to be laid out. Which is the part I need help with.

(well, basically I need something quite flexible as UILabel inside each polygon, but here I am struggling with the most basic operation)

Here is my MKPolygonRenderer subclass implementation:

static UIFont * font = nil;

@implementation MyMapOverlayRenderer

-(void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context
{
    MKMapRect theMapRect = [self.overlay boundingMapRect];

    if (!(MKMapRectIntersectsRect(mapRect, theMapRect)))
        return;

    NSString * t= @"Test" ;
    CGRect theRect = [self rectForMapRect:theMapRect];

    CGPoint point = CGPointMake((theRect.origin.x + theRect.size.width)/2, (theRect.origin.y + theRect.size.height)/2);        

    if (!font)
    {
        font = [UIFont fontWithName:@"Helvetica" size:20.0];
    }

    CGContextSaveGState(context);
    CGContextSetTextDrawingMode(context, kCGTextFillStroke); // This is the default

    [[UIColor blackColor] setFill];
    [t drawAtPoint:point withAttributes:@{NSFontAttributeName:font}];

    CGContextRestoreGState(context);
}

@end

Somehow, no text is drawn here.

My other observations:

  • point contains very large values. I tried replacing with (15,15), (5,5) and so on but to no success. I want the text at the center of polygon if possible.
  • I do not know if I have the drawAtPoint code right.
  • I had the same result when font was not static.
  • From my view controller I am setting fillColor property of the polygon overlay view, which works fine otherwise with MKPolygonRenderer, but not with my derived subclass MyMapOverlayRenderer. I see no fill color for the polygon. Where can I set it in the subclass?
0

There are 0 best solutions below