iosobjective-cmapkitalphamktileoverlay

iOS - Workaround MKOverlayRenderer and Alpha property


How can I access and modify the alpha property of an MKOverlayRenderer after it has already been passed to the Map?

Indeed I can modify the alpha attribute in the rendererForOverlay method :

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id <MKOverlay>)overlay
{
    if ([overlay isKindOfClass:[MKTileOverlay class]]) {

        MKTileOverlayRenderer *renderer = [[MKTileOverlayRenderer alloc] initWithTileOverlay:overlay];
        renderer.alpha = 0.6;
        return renderer;
    }
    return nil;
}

But this method is only called when I add an Overlay to the mapView right?

So my question is : Is there a way to have the possibility to change and set the value of this alpha attribute even after my Overlay is already rendered on the map ?

I tried to add the renderer to a NSMutableArray with :

 - (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id <MKOverlay>)overlay
{
    if ([overlay isKindOfClass:[MKTileOverlay class]]) {
        _allRenderer = [[NSMutableArray alloc] init];
        MKTileOverlayRenderer *renderer = [[MKTileOverlayRenderer alloc] initWithTileOverlay:overlay];

        renderer.alpha = 0.0;
        [_allRenderer addObject:renderer];
        return renderer;
    }
    return nil;
}

And then I can change the transparency by calling this method I made :

-(void)changeAlpha:(NSUInteger)index : (BOOL)isOpaque {

    if (isOpaque)
        [[_allRenderer objectAtIndex:index] setAlpha:0.0];
    else
        [[_allRenderer objectAtIndex:index] setAlpha:1.0];
}

Are there any better ways to do this ? It's seems kind of a slow process just to set the alpha value.

Actually I would like to switch from 0 to 1 the alpha value of my OverlayRenderer dynamically, so that I can display 1 Overlay, hide it, display an other one, hide it etc...


Solution

  • So basically what you can do is: rather than trying to set different alpha.

    You can toggle between having an overlay and removing it and having an initial value for alpha.

    For reference, please try this link: https://github.com/mapbox/mbxmapkit/issues/7

    Also this: https://github.com/mapbox/mbxmapkit/issues/39