Select to view content in your preferred language

Event thrown when map is FINISHED loading?

5682
4
11-05-2010 10:56 AM
GregSpiridonov
Occasional Contributor
I can't seem to find an event or any other way to tell when a map is completely finished loading in FLEX, (ie: when all layers content has been rendered to the screen and nothing else is loading)

I require this for printing functionality... When i try to use the load event to capture a screenshot of the map (BitmapData) i get a blank image.. if i tie the same function to a button, and manually press it, the image shows the full map.. I do not want the users to have to press multiple buttons to print..

any idea's or help?
Tags (2)
0 Kudos
4 Replies
Drew
by
Frequent Contributor
I don't see such an event in the docs either. It would be nice too.

Below is some code I wrote that should solve the problem. It seems like a lot but it works.

<?xml version="1.0" encoding="utf-8"?>
<s:Application name="Test"
      xmlns:fx="http://ns.adobe.com/mxml/2009"
      xmlns:s="library://ns.adobe.com/flex/spark"
      xmlns:mx="library://ns.adobe.com/flex/mx" 
      xmlns:esri="http://www.esri.com/2008/ags" >
 
 <fx:Script>
  <![CDATA[
   
   //IMPORTS
   import com.esri.ags.events.LayerEvent;
   import com.esri.ags.events.MapEvent;
   import com.esri.ags.layers.Layer;
   import mx.collections.ArrayCollection;
   import mx.controls.Alert;

   //-----------------------------------------
   //PRIVATE VARIABLES
   //-----------------------------------------
   private var _isLayersLoaded:Boolean = false;
   private var _layerLoadCount:int = 0;
   

   // isLayersLoaded getter
   // Checks if the private varibale _layerLoadCount == the total map layers in the map
   public function get isLayersLoaded():Boolean
   {
    var loaded:Boolean; 
    var  mapLayers:ArrayCollection = this.map.layers as ArrayCollection;
    if (mapLayers.length == this._layerLoadCount)
     loaded = true;
    else 
     loaded= false;
    return loaded;
   }
   
   
   //-----------------------------------------
   // on Map Load loop through all the layers and check if loaded. 
   // if not loaded, listen for the load event to be called.
   //-----------------------------------------
   private function _onMapLoad(event:MapEvent):void
   {
    for each (var lyr:Layer in this.map.layers)
    {
     if (lyr.loaded)
      _layerLoadCount++;
     else
      lyr.addEventListener(LayerEvent.LOAD, _onLayerLoad);
    }
   }
   //-----------------------------------------
   // if layer load event is called, add 1 to the _layerLoadCount variable.
   //-----------------------------------------
   private function _onLayerLoad(event:LayerEvent):void
   {
    _layerLoadCount++;
   }
   
   //-----------------------------------------
   // Test the variable isLayersLoaded
   //-----------------------------------------
   private function onPrintClick():void
   {
    if (this.isLayersLoaded == false)
     Alert.show("please wait, all layers are not yet loaded");
    else
     Alert.show("Layers Loaded!");
   }
  ]]>
 </fx:Script>

 <esri:Map id="map" load="_onMapLoad(event)" top="76" bottom="10" left="9" right="10">
  <esri:ArcGISTiledMapServiceLayer url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_Imagery_World_2D/MapServer/"/>
  <esri:ArcGISTiledMapServiceLayer url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_ShadedRelief_World_2D/MapServer/"/>
  <esri:ArcGISTiledMapServiceLayer url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer/"/>
 </esri:Map>
 <s:Button x="10" y="47" label="Button" id="btnPrint" click="onPrintClick();"/>
 
</s:Application>


if you need further assistance feel free to ask..

Drew
0 Kudos
Ganael_Jatteau
Emerging Contributor
Hi

I re-open that old thread concerning the load event on layers.
I'm using the API 2.5 and when the layer fires LayerEvent.LOAD, the tiles on a  ArcGISTiledMapServiceLayer are not finished drawing.

Is there any solution to know when all the layers finish to draw?

Cheers
0 Kudos
IvanBespalov
Frequent Contributor
LayerEvent.UPDATE_END
event.updateSuccess

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
      xmlns:s="library://ns.adobe.com/flex/spark" 
      xmlns:mx="library://ns.adobe.com/flex/mx" xmlns:esri="http://www.esri.com/2008/ags">
 
 <!-- Adobe Flex SDK 4.6.0 -->
 <!-- ArcGIS API for Flex 3.0 prerelease 15.03.2012 -->
 <!-- web.zone.ee/bespiva/layerevents -->
 
 <s:layout>
  <s:VerticalLayout paddingBottom="10" 
        paddingLeft="10" 
        paddingRight="10" 
        paddingTop="10" 
        gap="10" />
 </s:layout>
 
 <esri:Map id="map">
  <esri:extent>
   <esri:WebMercatorExtent id="lowerManhatten"
         minlon="-74.03" minlat="40.70" maxlon="-73.99" maxlat="40.72"/>
  </esri:extent>
  <esri:ArcGISTiledMapServiceLayer name="Base Layer" 
           url="http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"
           load="onLayerLoad(event)"
           loadError="onLayerLoadError(event)"
           updateStart="onLayerUpdateStart(event)"
           updateEnd="onLayerUpdateEnd(event)"/>
  <esri:ArcGISDynamicMapServiceLayer name="Census Block Points" 
             url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/"
             visibleLayers="{new ArrayCollection([0])}"
             load="onLayerLoad(event)"
             loadError="onLayerLoadError(event)"
             updateStart="onLayerUpdateStart(event)"
             updateEnd="onLayerUpdateEnd(event)"/>
  <esri:ArcGISDynamicMapServiceLayer name="Census Block Group" 
             url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/"
             visibleLayers="{new ArrayCollection([1])}"
             load="onLayerLoad(event)"
             loadError="onLayerLoadError(event)"
             updateStart="onLayerUpdateStart(event)"
             updateEnd="onLayerUpdateEnd(event)"/>
  <esri:ArcGISDynamicMapServiceLayer name="Coarse Counties" 
             url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/"
             visibleLayers="{new ArrayCollection([3])}"
             load="onLayerLoad(event)"
             loadError="onLayerLoadError(event)"
             updateStart="onLayerUpdateStart(event)"
             updateEnd="onLayerUpdateEnd(event)"/>
  <esri:ArcGISDynamicMapServiceLayer name="Detailed Counties" 
             url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/"
             visibleLayers="{new ArrayCollection([4])}"
             load="onLayerLoad(event)"
             loadError="onLayerLoadError(event)"
             updateStart="onLayerUpdateStart(event)"
             updateEnd="onLayerUpdateEnd(event)"/>
 </esri:Map>
 
 <s:Label text="Logs:" />
 
 <s:VGroup width="100%"
     height="100%">
  
  <s:TextArea width="100%"
     height="100%"
     id="log" />
  
 </s:VGroup>
 
 <fx:Script>
  <![CDATA[
   import com.esri.ags.events.LayerEvent;
   
   import mx.collections.ArrayCollection;
   import mx.utils.StringUtil;
   
   private var loadedCount:int;
   private var nonLoadedCount:int;
   private var updateStartedCount:int;
   private var updateEndedCount:int;
   
   private function addLog(message:String):void
   {
    log.text = StringUtil.substitute("{0}\n\n{1}", message, log.text);
   }
   
   protected function onLayerLoadError(event:LayerEvent):void
   {
    addLog(StringUtil.substitute("Error on loading layer {0}", event.layer.name));
    nonLoadedCount++;
    
    var mapLayersCount:int = ArrayCollection(map.layers).length;
    if (mapLayersCount == (nonLoadedCount + loadedCount))
    {
     addLog(StringUtil.substitute("Loaded: {0} of {1} layers\n {2} layers fault on loading.", 
      loadedCount, mapLayersCount, nonLoadedCount));
    }
   }
   
   protected function onLayerLoad(event:LayerEvent):void
   {
    addLog(StringUtil.substitute("{0} is loaded", event.layer.name));
    loadedCount++;
    
    var mapLayersCount:int = ArrayCollection(map.layers).length;
    if (mapLayersCount == (nonLoadedCount + loadedCount))
    {
     addLog(StringUtil.substitute("Loaded: {0} of {1} layers\n {2} layers fault on loading.", 
      loadedCount, mapLayersCount, nonLoadedCount));
    }
   }

   protected function onLayerUpdateStart(event:LayerEvent):void
   {
    addLog(StringUtil.substitute("{0} update started", event.layer.name));
    if (updateStartedCount == updateEndedCount)
    {
     updateStartedCount = updateEndedCount = 0; // reset
    }
    
    updateStartedCount++;
   }


   protected function onLayerUpdateEnd(event:LayerEvent):void
   {
    addLog(StringUtil.substitute("{0} update ended >> update success = {1}", 
     event.layer.name, event.updateSuccess));
    updateEndedCount++;
    
    if (updateStartedCount == updateEndedCount)
    {
     addLog("All layers are updated");
    }
   }

  ]]>
 </fx:Script>
 
</s:Application>
0 Kudos
Ganael_Jatteau
Emerging Contributor
Thanks for your answer! Listening LayerEvent.UPDATE_END works.

It would be nice to have access to a property Layer.updated. Without listening an event on the layer, it's not possible to know whether it's ready or not.
0 Kudos