Overlay not appearing on map

837 views Asked by At

I am trying to add a polygon overlay on to an MKMapKit map. The Map appears - but the polygon does not... Am I missing something obvious?

Code attached:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.mapView.showsPointsOfInterest = NO;

    CLLocationCoordinate2D worldCoords[4] = { {43,-100}, {43,-80}, {25,-80}, {25,-100} };
    MKPolygon *worldOverlay = [MKPolygon polygonWithCoordinates:worldCoords count:4];

    [self.mapView addOverlay:worldOverlay level:MKOverlayLevelAboveRoads];
}

And the Renderer class...

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
    if (![overlay isKindOfClass:[MKPolygon class]]) {
        return nil;
    }
    MKPolygon *polygon = (MKPolygon *)overlay;
    MKPolygonRenderer *renderer = [[MKPolygonRenderer alloc] initWithPolygon:polygon];
    renderer.fillColor = [[UIColor darkGrayColor] colorWithAlphaComponent:0.4];
    return renderer;
}

Any help would be appreciated!

Thanks Guy

1

There are 1 answers

5
guyh92 On

I have stumbled across the answer!

After Anna's help in the comments above helped me to get the layer working - it seemed that you must initiate the mapView after adding the layer. so after

[self.mapView addOverlay:worldOverlay level:MKOverlayLevelAboveRoads];

you need

mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];