Select to view content in your preferred language

How do I clear (or replace) the basemap with a different type of mapLayer? in AS3

720
3
04-18-2013 09:53 AM
stevemclaughlin
Occasional Contributor
In this example, I load an ArcGISDynamicMapServiceLayer as the basemap,
then clear it (removeAllLayers) then add an ArcGISDynamicMapServiceLayer as the basemap and it doesn't show.

In the example below, click on button 'Add dynamic map'
then button 'Clear Layers'
then button 'Add tiled'
and tiled map does not show.



If I do it the reverse order, then it works.

full code here:

<?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:esri="http://www.esri.com/2008/ags"
      >
 <fx:Script>
  <![CDATA[
   import com.esri.ags.events.MapEvent;
   import com.esri.ags.layers.ArcGISDynamicMapServiceLayer;
   import com.esri.ags.layers.ArcGISTiledMapServiceLayer;
   
   import mx.collections.ArrayList;
   
   private var worldurl:String = "http://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer";
   private var topourl:String = "http://server.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer";
   private var world2:String = "http://server.arcgisonline.com/ArcGIS/rest/services/Reference/World_Boundaries_and_Places/MapServer";
   private var dynamicurl:String ="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer";
   
   private function createDynamicLayer():void {
    trace('createDynamicLayer()');
    var dynamicMap:ArcGISDynamicMapServiceLayer = new ArcGISDynamicMapServiceLayer(dynamicurl);
    dynamicMap.visibleLayers = new ArrayList([5]);  // just the states layer
    mymap.addLayer(dynamicMap);
   }
   private function createTiledLayer():void {
    trace('createTiledLayer()');
    var tiledMap:ArcGISTiledMapServiceLayer = new ArcGISTiledMapServiceLayer(worldurl );
    mymap.addLayer(tiledMap);
   }
   private function addTopoMap():void {
    trace('addTopoMap()');
    var tiledMap:ArcGISTiledMapServiceLayer = new ArcGISTiledMapServiceLayer(topourl );
    mymap.addLayer(tiledMap);
   }
   private function clearLayers():void {
    mymap.removeAllLayers();
   }
   
   protected function layerAddHandler(event:MapEvent):void
   {
    trace('Layer added');
   }
   
  ]]>
 </fx:Script>
 
 <fx:Declarations>
  <esri:WebMercatorExtent id="usa" minlon="-124.493523" minlat="18.295270" maxlon="-62.135124" maxlat="53.841627"/>
 </fx:Declarations>
 
 <esri:Map id="mymap" extent="{usa}" wrapAround180="true"  layerAdd="layerAddHandler(event)"/>
 <s:HGroup  top ="20" left="100"  gap="20">
   <s:Button label="Add dynamic map" click="createDynamicLayer( )"  />
   <s:Button label="Clear Layers" click="clearLayers( )"  />
   <s:Button label="Add tiled" click="createTiledLayer()"  />
   <s:Button label="Add topo map" click="addTopoMap( )"  />
 </s:HGroup>
</s:Application>
Tags (2)
0 Kudos
3 Replies
RobertScheitlin__GISP
MVP Emeritus
Steve,

   You need to set the maps LODs when you add a tiled layer:

            private function createTiledLayer():void {
                trace('createTiledLayer()');
                var tiledMap:ArcGISTiledMapServiceLayer = new ArcGISTiledMapServiceLayer(worldurl );
                mymap.addLayer(tiledMap);
                tiledMap.addEventListener(LayerEvent.LOAD, function():void{
                    mymap.lods = tiledMap.tileInfo.lods;
                });
            }
0 Kudos
stevemclaughlin
Occasional Contributor
Thanks!  awesome - it works great!  I would have never figured this out...
  Thank goodness rscheitlin is active on these forums!

I should have looked at 'switching basemaps example' at
http://resources.arcgis.com/en/help/flex-api/samples/#/Switching_Basemaps/01nq0000002s000000

it explains that you need to update the LODs/zoomslider to use/show the levels for the selected base map

thanks again..
steve
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Steve,

Glad to help. Now just don't forget that important step 1 in the graphics below:

0 Kudos