Select to view content in your preferred language

Basemap Switching

1802
1
03-22-2013 09:09 AM
williambich
Emerging Contributor
I am trying to change the basemap for the silverlight viewer.  The problem is everytime I change the basemap layer, the gallery stops responding.  The Gallery (basemap switcher OOB) tries to work by adding is selected layer to the bottom of the Layer stack, and leaves the basemap that was already there.  So the MapApplication.Current.Map.Layers collection now has two basemaps and the one just selected (by the gallery functionality) can not be seen. I am thinking that its because I am not creating the basemap the way I should.  Here is the code I use to define and add the basemap.

//Removes the basemap
MapApplication.Current.Map.Layers.RemoveAt(0);

//Creates a layer
ArcGISTiledMapServiceLayer lyr = new ArcGISTiledMapServiceLayer();

//Sets properties
lyr.ID = "Topographic";
lyr.Url = "http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer";

//Adds the basemap
MapApplication.Current.Map.Layers.Insert(0, lyr);


After this code runs, everything is ok, but the gallery can't determine the bottom layer is the basemap.  If the user decides to use the "Gallery" functionality, the Basemap Switcher in the Gallery actually slaps its basemap on the bottom, and does not remove the basemap I added.  Therefore You can't see the map it just switched too.  There must be something I messed up, when constructing the Layer for the basemap.  The application works great, as long as I am not trying to switch basemaps myself.  The Gallery will switch basemaps all day long, without an issue.

I am working on bookmark functionality.
0 Kudos
1 Reply
williambich
Emerging Contributor
Ok, for everyone interested. I am answering my own question.  I have overcome this issue, its not very elegant, but it works. 

The problem existed, unsure if it was a bug with the Basemap switcher OOB (gallery) or how we have changed the product to fit our needs.  My guess is it was us.

We created a reference to the map in one of our Addins.  Because we did this, the reference becomes our map.  We must listen and react to the events that happen in the ESRI API.  This is where I think we went wrong.  I am not sure of the exact place we did not pay close enough attention, but when we tried to change the basemap the switcher did not like it, therefore it could not change basemaps anymore.

To fix the issue:
//Event to capture when Layers have been initialized
//This is a fix, to also check for multiple basemaps
_map.Layers.LayersInitialized += new LayerCollection.LayersInitializedHandler(Layers_LayersInitialized);

This event fires once.  Its in "Layers_LayersInitialized" where I look for the basemaps, if I see that I have two basemaps (this is when the basemap switcher is trying to change maps) I remove Element 1 (the switcher will always put its basemap at the very bottom).

My code looks like:

              if (MapApplication.Current.Map.Layers.Count > 1)
              {
                  if ((MapApplication.Current.Map.Layers[1].GetType() == typeof(ArcGISTiledMapServiceLayer)))
                  {
                      MapApplication.Current.Map.Layers.RemoveAt(1);
                  }
                  else if ((MapApplication.Current.Map.Layers[1].GetType() == typeof(ESRI.ArcGIS.Client.Bing.TileLayer)))
                  {
                      MapApplication.Current.Map.Layers.RemoveAt(1);
                  }
              }

Its not pretty, but it works.  Its been almost a week since I posted the initial post.  So I just figured no one knew enough about the issue to make a logical comment.  I use ArcGISTiled and Bing to use as my basemaps, so If I see either one of those types on element 1 I remove that layer.  I figured out that when seeing the problem, then opening up the Immediate Pane in Visual Studio and physically removed the offending layer (that worked).  You can also see this issue if you zoom in.. really far, then zoom out really far, the viewer has to try and download the tiles and you can (for a brief second) see the other layer peeking through.

I know I can do this better, I just don't know how.  30 some.. people has seen this post as of: 3/29/2013, just wanted to put the extra comments up here if anyone else is facing this sort of problem.
0 Kudos