2

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...

Funnybear
  • 71
  • 11

2 Answers2

1

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

Annie Gupta
  • 2,786
  • 15
  • 23
0

Ok so I worked with adding/removing the MKTileOverlay instead of playing with the alpha value, as @Sharpkits mentioned in the comments.

I almost got the expected result. I'm just facing an issue with the first time the tiles are rendered, I got some glitches, because the rendering is tile by tile.

I think I should display a spinner for the first 2 seconds while the Overlays are rendering.

After that delay, my animation with the tile images is playing smoothly.

I still got the glitch effect when I zoom in/out, because new tiles have to be rendered.

Some context

Basically I would like to make an animation with 10 images. All the images are cut according to specific zoom level, specific positions and with 15 minutes time interval.

I have a UISlider and a Play/Pause Button, and I'm removing or adding specific Overlay, according to the position of my Slider while the animation is playing. With tapGestureRecognizer on my Slider, I can go back and further in the "video" while I'm displaying the Overlay that has to be displayed.

-(void)showOverlay:(NSUInteger)index {

    for (int i = 0; i < [_allTileOverlay count]; i++) {
        if (i != index)
            [_mapView removeOverlay:[_allTileOverlay objectAtIndex:i]];
        else
            [_mapView addOverlay:[_allTileOverlay objectAtIndex:index]];
    }
}

And this is the part where I'm displaying the Overlay according to the position of my UISlider while the animation is playing.

if ([_allTileOverlay count]>0) {
        if (_currentSlider.value>=[_allTileOverlay count]-1) {
            dispatch_async(dispatch_get_main_queue(), ^(void){
                //Run UI Updates
                [self showOverlay:0];
            });
            _currentSlider.value=0;
        }else{
            //scroll to desire position
            _currentSlider.value++;
            dispatch_async(dispatch_get_main_queue(), ^(void){
                //Run UI Updates
                [self showOverlay:_currentSlider.value];
            });
        }
    }

There is some work to do in order to finish the feature, but I will close this question because I got all the informations I expected.

Funnybear
  • 71
  • 11