LeanneGamble LeanneGamble - 2 months ago 22
iOS Question

Using viewForOverlay to return an MKOverlayView but I also need to return an MKCircleRenderer in the same method

I have an

MKMapView
that I need to display two types of overlay on. One is a tiled overlay that I use a subclass of
MKOverlayView
and an
MKCircleRenderer
.

My method is:
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
but now I need to also render an
MKCircle
I'm getting an compiler error of:
'Incompatible pointer types returning 'MKCircleRenderer *' from a function with result type 'MKOverlayView * _Nonnull'.

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{

if ([overlay isKindOfClass:[MKCircle class]]) {
MKCircleRenderer *circleView = [[MKCircleRenderer alloc] initWithOverlay:overlay];
circleView.strokeColor = [UIColor redColor];
circleView.fillColor = [[UIColor redColor] colorWithAlphaComponent:0.4];
return circleView;
} else {
TileOverlayView *view = [[TileOverlayView alloc] initWithOverlay:overlay];
view.tileAlpha = 1.0;
return view;
}
}


That is my code, I'm aware that
initWithOverlay
is deprecated which I'm working on as another issue.

Rob Rob
Answer

The issue is that the tile view is apparently subclassing from MKOverlayView (returned by viewForOverlay), whereas MKCircleRenderer subclasses from the more modern MKCircleRenderer, a subclass of MKOverlayRenderer (returned by rendererForOverlay), not of MKOverlayView.

If you can make your tile view a MKOverlayRenderer subclass rather that an overlay view, then you can implement rendererForOverlay and return either circle renderer views or tile view rather than viewForOverlay.

Comments