Select to view content in your preferred language

Flex API 2.0, request when Layer visibility set to false

1893
0
09-02-2010 08:34 AM
ReneRubalcava
Frequent Contributor II
Recently I noticed in the 2.0 API, there is no more LayerEvent.TILES_UPDATED, just a LayerEvent.UPDATE_END, which is great. Simplifies things quite a bit.

When needing to manage Layers, i.e. - when they load and are complete, it would be great if the Flex API could dispatch a LayerEvent.UPDATE_END when layer.visible = false is peformed. I realize that visible is inherited from the UIComponent, but as of right now if you want to know that layer.visible has been set to false, you need to listen for FlexEvent.HIDE, but when layer.visible is set to true, you'll still get a LayerEvent.UPDATE_END event.

That would mean that to manage visible layers in real time, I'd need to listen to 2 events from the same layer to do the same thing.

Here is a code snippet that I currently use to deal with this situation.
map.addEventListener(MapEvent.LAYER_ADD, map_layerAddHandler)
protected var lyrCount:uint;
protected var lyrUpdateCount:uint;
protected function map_layerAddHandler(event:MapEvent):void {
 var lyr:Layer = event.layer;
 lyr.addEventListener(LayerEvent.UPDATE_END, onLayerUpdateEnd_handler);
 lyr.addEventListener(FlexEvent.HIDE, function(e:Event):void{trace(e)});
 lyr.addEventListener(FlexEvent.HIDE, onHide);
 if (lyr.visible)
  lyrCount++;
}
// I need to listen for the Hide event and manually dispatch a LayerEvent
// in order to have a proper count of current visible layers
protected function onHide(event:FlexEvent):void {
 if (event.currentTarget is Layer) {
  event.currentTarget.dispatchEvent(new LayerEvent(LayerEvent.UPDATE_END, event.currentTarget as Layer, null, true, false));
 }
}
protected function onLayerUpdateEnd_handler(event:LayerEvent):void {
 if (event.updateSuccess) {
  lyrUpdateCount++;
  var lyr:Layer
  var tmpCount:uint = lyrCount;
  lyrCount = 0;
  for each (lyr in this.layers) {
   if (lyr.visible)
    lyrCount++;
  }
  trace("lyrCount",lyrCount);
  trace("lyrUpdateCount",lyrUpdateCount);
  trace("tmpCount",tmpCount);
  if (lyrUpdateCount == lyrCount || lyrCount != tmpCount) {
   lyrUpdateCount = 0;
  }
 }
}


It's really more of a convenience request, but it'd be nice to have.
I figure since you're working on API 2.1, it couldn't hurt to ask.

Thanks.
Tags (2)
0 Kudos
0 Replies