Greetings Everyone, I am working on a tool to save projects or sessions in the Flex Viewer application. I am pretty close at this point but I am having a problem with the tiled and dynamic map services overlaying incorrectly. When I load the map settings for each different session, the dynamic layers do not initially overlay the tiled layers correctly. The tiled layers are always significantly offset from the dynamic layers (notice how the building footprints and stereets do not overlay the ortho photo proprely). When I reload the session the second time however, everything aligns correctly. This leaves me to believe that I am calling a method out of order when a tiled layer is turned on. I have included my code below and attached a graphic of the misaligned dynamic and tiled layers. Does anyone recognize any errors? Thanks, Tyler Waring
public function loadSavedSession(session:Object):void
{
SiteContainer.addEventListener(AppEvent.SAVED_SESSION_APPLIED, SavedSearchCleanUpSteps)
//get service and layer visability from the session object
var strAllFinalVisibleServicesAndArrays:String = session.strVisLayers;
var aryAllVisibleServicesAndLayers:Array = strAllFinalVisibleServicesAndArrays.split("!@!");
trace("Session visible Layers = " + session.strVisLayers)
var aryServiceLayerNames:Array = new Array;
//create an array of visible services
for (var i:int = 0; i<aryAllVisibleServicesAndLayers.length; i++)
{
trace("session layers + " + aryAllVisibleServicesAndLayers);
var strServiceArray:String = aryAllVisibleServicesAndLayers;
var serviceArray:Array = strServiceArray.split("|");
aryServiceLayerNames.push(serviceArray[1]);
}
//loop over layers and change their visibility
var intServiceLayerIndex:int;
var myDynamicServiceLayer:ArcGISDynamicMapServiceLayer;
var myTiledServiceLayer:ArcGISTiledMapServiceLayer;
var serviceName:String;
var strLayers_alpha:String;
var aryLayersName:Array = new Array;
var aryLayersAlpha:Array = new Array;
var ext:Extent = new Extent(session.xmin, session.ymin, session.xmax, session.ymax);
for each(var layer:Layer in map.layers)
{
var strLayerSettings:String;
var aryLayerSettings:Array;
var acExistingVisibleLayers:ArrayCollection;
var aryExistingVisibleLayers:Array;
var acNewVisibleLayers:ArrayCollection;
var strNewVisibleLayers:String;
var aryNewVisibleLayers:Array;
var blDispatchVisibilityChanged:Boolean = false;
var nmTurnedOffID_ServiceName:String;
if (layer is ArcGISDynamicMapServiceLayer)
{
myDynamicServiceLayer = ArcGISDynamicMapServiceLayer(layer);
serviceName = getServiceNameFromURL(myDynamicServiceLayer.url);
intServiceLayerIndex = aryServiceLayerNames.indexOf(serviceName);
//if the layer is turned on in the session object then...
if (intServiceLayerIndex >-1)
{
//get the settings for this service as they are defined by the session object.
strLayerSettings = aryAllVisibleServicesAndLayers[intServiceLayerIndex];
//create an array these settings.
aryLayerSettings = strLayerSettings.split("|");
trace("aryLayerSettings = " + aryLayerSettings.toString())
// if this layer is not visible then turn it on.
if (myDynamicServiceLayer.visible == false)
{
myDynamicServiceLayer.visible = true;
}
//get a list of layers that are currently visible for this service
acExistingVisibleLayers = myDynamicServiceLayer.visibleLayers;
aryExistingVisibleLayers = acExistingVisibleLayers.source;
//get the layers that are visible for this serivce as defined in the session object.
strNewVisibleLayers = aryLayerSettings[3];
aryNewVisibleLayers = strNewVisibleLayers.split(",");
//convert the array of visible layers from strings to integers
var aryFinalVisibleLayers:Array = new Array
for (var intVisLyr:int = 0; intVisLyr < aryNewVisibleLayers.length; intVisLyr++)
{
aryFinalVisibleLayers.push(int(aryNewVisibleLayers[intVisLyr]))
}
//convert the integer array of visible layers to an array collection
acNewVisibleLayers = new ArrayCollection(aryFinalVisibleLayers);
trace("aryExistingVisibleLayers = " + aryExistingVisibleLayers.toString() + " aryNewVisibleLayers = " + aryFinalVisibleLayers.toString())
//if the visiblity of the layers in this service are not the same as the visibility of the layers in the session object...
if(myDynamicServiceLayer.visibleLayers != acNewVisibleLayers)
{
//set the visible layers in this service = to the visible layers in the session object.
myDynamicServiceLayer.visibleLayers = acNewVisibleLayers;
myDynamicServiceLayer.refresh();
blDispatchVisibilityChanged = true;
}
//change the alpha value of this service if it differs from the alpha value recorded in the session object.
if (layer.alpha != aryLayerSettings[2])
{
layer.alpha = aryLayerSettings[2];
//the following event is listened for in LiveMapsWidget.mxml in the iMaps directory
SiteContainer.dispatchEvent(new AppEvent(AppEvent.LAYER_ALPHA_CHANGED, false, false));
}
//if the visibility was changed then send out messages to other widgets to update their settings.
if (blDispatchVisibilityChanged == true)
{
for (var iNew:int = 0; iNew<aryFinalVisibleLayers.length; iNew++)
{
if (aryExistingVisibleLayers.indexOf(aryFinalVisibleLayers[iNew])== -1)
{
nmTurnedOffID_ServiceName = aryFinalVisibleLayers[iNew]+"_"+serviceName;
trace(nmTurnedOffID_ServiceName);
SiteContainer.dispatchEvent(new AppEvent(AppEvent.LAYER_VISIBILITY_CHANGED, false, false, nmTurnedOffID_ServiceName))
}
else
{
aryExistingVisibleLayers.splice(aryExistingVisibleLayers.indexOf(aryFinalVisibleLayers[iNew]),1)
}
}
for (var iExisting:int = 0; iExisting<aryExistingVisibleLayers.length; iExisting++)
{
nmTurnedOffID_ServiceName = aryExistingVisibleLayers[iExisting]+"_"+serviceName;
trace(nmTurnedOffID_ServiceName);
SiteContainer.dispatchEvent(new AppEvent(AppEvent.LAYER_VISIBILITY_CHANGED, false, false, nmTurnedOffID_ServiceName))
}
}
}
else //if this service layer is not listed in the session object then turn it off.
{
layer.visible = false;
}
}
else if (layer is ArcGISTiledMapServiceLayer)
{
myTiledServiceLayer = ArcGISTiledMapServiceLayer(layer);
serviceName = getServiceNameFromURL(myTiledServiceLayer.url);
intServiceLayerIndex = aryServiceLayerNames.indexOf(serviceName);
trace(aryServiceLayerNames.toString())
//if this service is on in the session object then turn it on
if (intServiceLayerIndex >-1)
{
myTiledServiceLayer.visible = true;
myTiledServiceLayer.refresh();
}
else
{
myTiledServiceLayer.visible = false;
myTiledServiceLayer.refresh();
}
}
}
SiteContainer.dispatchEvent(new AppEvent(AppEvent.SAVED_SESSION_APPLIED, false, false));
map.extent = ext;
}