Select to view content in your preferred language

Sample Flex Viewer - customizing for multiple Live Maps and Layer Visibility

2161
3
06-02-2010 08:36 AM
JamesHolstead
Emerging Contributor
My project involves a lot of mapservices containing many layers, having it all displayed in the livemaps widget is unpratical.

My two goals are:
1. customize the live maps widget to display once for every mapservice, instead of all in the same window/TOC.
2. override the default layer visibility within the TOC.


My ideal livemaps config would look like:
<livemaps>
 <mapservice label="Map1" visibleLayers="streets,buildings" menu="Navigation" type="dynamic" visible="false" alpha="1" icon="com/esri/solutions/flexviewer/assets/images/icons/i_highway.png">http://arcserver/ArcGis/rest/services/Map1/MapServer</mapservice>
 <mapservice label="Map2" visibleLayers="population" menu="Resources" type="dynamic" visible="false" alpha="1" icon="com/esri/solutions/flexviewer/assets/images/icons/i_highway.png">http://arcserver/ArcGis/rest/services/Map2/MapServer</mapservice>
</livemaps>
added 'menu' and 'visibleLayers' attributes

<widget label="Live Maps" livemap="Map1" icon="com/esri/solutions/flexviewer/assets/images/icons/i_folder.png" menu="menuMap" config="com/esri/solutions/flexviewer/widgets/LiveMapsWidget.xml">com/esri/solutions/esa/widgets/LiveMapsWidget.swf</widget>
added attribute 'livemap'


Below is a change in the LiveMapsWidget.mxml, rewriting the getLayers() method:
// in init()
layerRepeater.dataProvider = getLayers(/* xml attribute livemap */);

private function getLayers(livemap:String):Array
   {
    var layerArray:Array = new Array();
    for(var i:Number = map.layerIds.length -1; i >= 0; i--)
    {
     var layer:Layer = map.getLayer(map.layerIds);
     if(!(layer is GraphicsLayer) && layer.name == livemap) //** added this condition to filter for only livemap
      layerArray.push(layer);
    }
    return layerArray;
   }



Would it be possible to get the confid data xml attribute 'livemap' into the widget above? perhaps there is a better way of going about this???



2. Looking into overriding which layers are visible by default I've come up empty. I'd have to modifiy the TOC object but am unsure exactly as to where it inherits the property from the mapservice. Any ideas here would be great.
Tags (2)
0 Kudos
3 Replies
RobertScheitlin__GISP
MVP Emeritus
James,

    This is very possible you just need these changes.

In your config.xml



In your LivemapsWidget.mxml

//in init
layerRepeater.dataProvider = getLayers(configData.configWidgets[getId()].livemap);

   private function getLayers(livemap:String):Array
   {
    var layerArray:Array = new Array();
    for(var i:Number = map.layerIds.length -1; i >= 0; i--)
    {
     var layer:Layer = map.getLayer(map.layerIds);
     if(!(layer is GraphicsLayer) && layer.name == livemap){ //** added this condition to filter for only livemap
      layerArray.push(layer);
      toc.includeLayers = livemap;
     }
    }
    return layerArray;
   }


Your configManager.as
//=================================================
    //widgets
    var configWidgets:Array = [];
    var wList:XMLList = configXML..widget;
    for (i = 0; i < wList.length(); i++)
    {
     var wLabel:String =wList.@label;
     var wIcon:String = wList.@icon;
     var wConfig:String = wList.@config;
     var wPreload:String = wList.@preload;
     var wUrl:String = wList;
     var wLiveMap:String = wList.@livemap;
     var widget:Object = 
     {
      id: i,
      label: wLabel,
      icon: wIcon,
      config: wConfig,
      preload: wPreload,
      url: wUrl,
      livemap: wLiveMap
     }
     configWidgets.push(widget);
    }
    configData.configWidgets = configWidgets;


The visible layers is a bit more difficult. I have worked on this but have trouble with the toc overriding the ones you want visible with the mapservices default visibility.
0 Kudos
JamesHolstead
Emerging Contributor
Had to set the includeLayers property:
toc.includeLayers = getLayers(configData.configWidgets[getId()].livemap);
layerRepeater.dataProvider = getLayers(configData.configWidgets[getId()].livemap);



And had to define getId() in BaseWidget.as:
                               /**
  * Get the widet ID. A widget ID is a internal generate identifier in number.
   * 
   * @return value the Number id.
   */
  public function getId():Number
  {
   return widgetId;
  }


Works great, thanks once again.  Will take a stab at the TOC mod.
0 Kudos
JamesHolstead
Emerging Contributor
This may do the trick, have not done extensive testing however.

Here i'm just looping through the ArcGISDynamicMapServiceLayer.layerInfos array and setting defaultVisibility to false. Of course you can easily pass perhaps a comma delimited string from the widget config data which can either represent visible or hidden layers.

private function init():void
   {
    //_servicesCollection = GetServiceTypes();
    toc.map = map;
    toc.excludeLayers = getBasemaps();
    var inclLayers:Array = getLayers(configData.configWidgets[getId()].livemap);
    for(var i:Number = 0; i < inclLayers.length; i++) {
     if(inclLayers is ArcGISDynamicMapServiceLayer) {
      var layer:ArcGISDynamicMapServiceLayer =  inclLayers;
      for(var j:Number = 0; j < layer.layerInfos.length; j++) {
       layer.layerInfos.defaultVisibility = false;
      }
     }
    }
    toc.includeLayers = inclLayers;
    toc.excludeGraphicsLayers = true;
    layerRepeater.dataProvider = getLayers(configData.configWidgets[getId()].livemap);
    //wTemplate.addTitlebarButton(ICON_URL + "i_about.png", "AddService", showServices);
    wTemplate.addTitlebarButton(ICON_URL + "i_options.png", "Layer Options", showStateOptions);
    wTemplate.addTitlebarButton(ICON_URL + "i_folder.png", "Layer Visibility", showStateVisibility);
   }
0 Kudos