Hide/Show AGSLayer

1567
7
Jump to solution
01-29-2013 02:44 PM
AaronConnolly
Occasional Contributor
Hi,

Prior to 10.1 when you wanted to toggle the visibility of  layers you had to retain a copy of a layer's UIView and use the UIView's hidden property. The release notes now say to NOT do this, but rather to use the visible property on AGSLayer.

I'm having trouble getting this to work. I add 3 layers to a map, then toggle either their visible property or the opacity property to (YES/NO, 0.0/1.0 respectively) to show / hide them. This does not work because the layers themselves sometimes won't load tiles until you interact with the map.

This is troublesome as I would expect the map to load it's tiles as soon as it's been made visible. Is that not the case? How should I go about toggling the layers visibility like this AND have the tile imagery load, without the map having moved previously?

In my example the streets layer should load because its the one that's visible at first. The Topo and Imagery layers, if you switch to them with the buttons below, will not display tiles until you move the map. Sometimes this works, sometimes it doesn't. For example on the iPhone simulator the Topo layer would not work, but the Imagery did work. On iPad simulator neither Topo nor Imagery would load until you moved the map.

I've attached a sample project demonstrating this issue.

Thanks,
- Aaron
0 Kudos
1 Solution

Accepted Solutions
DanaMaher
Occasional Contributor
Dana,

I would rather just control the visibility in this manner per the release notes and as such want to know why this doesn't work. Moving the map ever-so-slightly seems like a hack.

...

To me it looks like any changes to the visibility property don't fire a new render even for the tile imagery in the layer. Same goes for the opacity property.


Yes, it is definitely a hack. Did you see my edit that you can achieve the same result with the - (void)refresh method of AGSLayer?

View solution in original post

0 Kudos
7 Replies
DanaMaher
Occasional Contributor
I checked out your demo and saw the behavior you are describing.

A quick fix is to very slightly shift the center of the map every time you toggle a layer. If you shift the center point by a very small multiplier, the map will update but not shift visibly. I am attaching a modified version of your demo that does such. I used a multiplier of 1.00001 and there is not visible map shift.

I find that the map renders more quickly if you just swap layers in and out of the map using addMapLayer: / removeMapLayer: . There does seem to be a cost to keeping track of empty layers on the map. This would also avoid your problem.

EDIT: Calling the -(void)refresh method of AGSLayer is a simpler solution. I should have seen that in the API docs. If you replace the map center shifting code in my edit of your test case with [layer refresh], you get the same results.
0 Kudos
AaronConnolly
Occasional Contributor
I checked out your demo and saw the behavior you are describing.

A quick fix is to very slightly shift the center of the map every time you toggle a layer. If you shift the center point by a very small multiplier, the map will update but not shift visibly. I am attaching a modified version of your demo that does such. I used a multiplier of 1.00001 and there is not visible map shift.

I find that the map renders more quickly if you just swap layers in and out of the map using addMapLayer: / removeMapLayer: . There does seem to be a cost to keeping track of empty layers on the map. This would also avoid your problem.


Dana,

Thanks for your reply. I thought about moving the map programmatically and about adding/removing the layers dynamically. I would rather just control the visibility in this manner per the release notes and as such want to know why this doesn't work. Moving the map ever-so-slightly seems like a hack. In my case I can't do this because I fetch records from a web service each time the map's extent changes. To do this I'd get an extra call and a few more redundant CPU cycles. This should work because that's why they gave us the visibility property, right?

To me it looks like any changes to the visibility property don't fire a new render even for the tile imagery in the layer. Same goes for the opacity property.
0 Kudos
DanaMaher
Occasional Contributor
Dana,

I would rather just control the visibility in this manner per the release notes and as such want to know why this doesn't work. Moving the map ever-so-slightly seems like a hack.

...

To me it looks like any changes to the visibility property don't fire a new render even for the tile imagery in the layer. Same goes for the opacity property.


Yes, it is definitely a hack. Did you see my edit that you can achieve the same result with the - (void)refresh method of AGSLayer?
0 Kudos
AaronConnolly
Occasional Contributor
Yes, it is definitely a hack. Did you see my edit that you can achieve the same result with the - (void)refresh method of AGSLayer?


Ah, I didn't see your edit. I'll probably go with that until ESRI responds. Thanks for the tip.
0 Kudos
DiveshGoyal
Esri Regular Contributor
Thank you for providing the testcase. We have investigated the issue and can confirm that it is a bug. We will fix it in our next release. In the meantime, you can continue to use the workaround of calling refresh after you change the layer visibility.
0 Kudos
AaronConnolly
Occasional Contributor
Great Divsesh. Thanks for the feedback.
0 Kudos
AaronConnolly
Occasional Contributor
Divesh, can you provide me with a bug number for this issue?

Thanks,
- Aaron
0 Kudos