1

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?
Nirav Bhatt
  • 6,940
  • 5
  • 45
  • 89
  • 1
    Don't worry about point values. They are in the virtual drawing space of the map not the physical screen. Some things are missing like: calling `super`, calling PushContext/PopContext, etc. See http://stackoverflow.com/questions/7825220/draw-text-in-circle-overlay for an example. –  Sep 16 '14 at 12:10
  • Ah! That simply worked out of the box. I tried push/save earlier but never together. The only problem now is it adds multiple text to single polygon despite my MKMapRectIntersectsRect clause. – Nirav Bhatt Sep 16 '14 at 13:58
  • @Anna could you please advise? Also please add your comment as answer I will mark as correct. – Nirav Bhatt Sep 16 '14 at 14:40
  • Advice on the "multiple text"? You might be getting "fat" text because of the CGContextSetTextDrawingMode call. Otherwise, please update your question with a picture of what's happening or more details. –  Sep 16 '14 at 17:58
  • @Anna it's not fat text, but multiple times call to drawMapRect function for single polygon (maybe its called for each tile within the polygon). Any ideas to avoid it? – Nirav Bhatt Sep 16 '14 at 21:24
  • Did you ever solve your problem? – Georg Jun 19 '15 at 07:17

0 Answers0