Select to view content in your preferred language

Change layer visibility onLoad

1392
8
08-26-2010 07:37 AM
GlenRhea
Emerging Contributor
I'm trying to change the visible layer displayed after the onLoad event for an ArcGISDynamicMapServiceLayer and it isn't working. I have a menu that works after the map is loaded but the layer that is visible initially is the one defined in the service.

There is too much code to post so here is a link to the app:
http://goo.gl/vHmK
0 Kudos
8 Replies
GlenRhea
Emerging Contributor
Wow, you used to be able to get help on ESRI's forums, apparently that isn't the case anymore. Too bad, they were a great resource.
0 Kudos
HaroldBostic
Frequent Contributor
THe link did not work for me
0 Kudos
derekswingley1
Deactivated User
Link works for me...

I took a quick look. Where do you add your dynamic layer to the map? I see where you create it:
//load basemap dynamic last so it's always on top
dn_layer = new esri.layers.ArcGISDynamicMapServiceLayer("http://" + mapserv + ".geostor.arkansas.gov/ArcGIS/rest/services/BASEMAP_DYNAMIC/MapServer",{id:"dyn_layer"});
dyn_layer.setOpacity(".5")

but I don't see where you add it to your map. Also, I can't get your dynamic layer to display from the services directory but I might be using the wrong URL:  http://www.geostor.arkansas.gov/ArcGIS/rest/services/Basemap_Dynamic/MapServer?f=jsapi

Finally, and this is just out of curiosity, what is the advantage to using your initLayer function rather than just creating and adding layers to your map?
0 Kudos
GlenRhea
Emerging Contributor
I was having problems with my AGS services over the weekend, everything should be working again now. I add the "app" layers here:
 if (getUrlParam("app")) {
  //ex, layer = APP_SALINE
  var layer = getUrlParam("app");
  user_layer = new esri.layers.ArcGISDynamicMapServiceLayer("http://" + mapserv + ".geostor.arkansas.gov/ArcGIS/rest/services/APPS/" + layer + "/MapServer",{id:"user_layer"});
  user_layer.setOpacity(".7")



The initLayer function is just a generic function I use in all my JS apps to keep things consistent across apps. It also encompasses all the different layer types in one function as well.

Also, I didn't get any email notifications from replies to this forum post because I didn't set that option. I should get an email as soon as someone replies this time, sorry for the delay.

Thanks for your replies.
Glen
0 Kudos
GlenRhea
Emerging Contributor
Also, the basemap_dynamic service doesn't have any layers turned on by default so you wouldn't see anything on the rest testing viewer. You can use the following viewer to view them if you like:
http://dev.geostor.arkansas.gov/g6/Viewer.html
0 Kudos
HaroldBostic
Frequent Contributor
Hello Glen:

What's happening in your code is this:

In your init you are doing this:
if (getUrlParam("app")) {
243 //ex, layer = APP_SALINE
244 var layer = getUrlParam("app");
245 user_layer = new esri.layers.ArcGISDynamicMapServiceLayer("http://" + mapserv + ".geostor.arkansas.gov/ArcGIS/rest/services/APPS/" + layer + "/MapServer",{id:"user_layer"});
246 user_layer.setOpacity(".7")
247 //we dont want identify for the districts app
248 if (!(layer.toLowerCase() == "app_districts")) {
249 dojo.connect(map, "onLayerAdd", mapLoaded);
250 } else if (layer.toLowerCase() == "app_districts" && getUrlParam("title")) {
251 console.log("districts & title");
252 var title = getUrlParam("title");
253 if (title.toLowerCase() == "rep") {
254 console.log("districts & title & rep");
255 // should be app_districts & hor viewer, set hor layer visible
256 dojo.connect(user_layer, "onLoad", function(evt){console.log("user_layer loaded");changeHorMap([3])});
257 //dojo.connect(user_layer, "onLoad", function(evt){console.log("user_layer loaded");user_layer.setVisibleLayers([3])});
258 //dojo.connect(user_layer, "onLoad", user_layer.setVisibleLayers([3]));
259 }
260 }
261 }


the above code, specifically
dojo.connect(user_layer, "onLoad", function(evt){console.log("user_layer loaded");changeHorMap([3])}); 
changes your visibile layer(s) to 3 which is the preferred layer (I assume)

subsequently, later in your init you do this
if (dyn_layer.loaded) {
271 buildLayerList(dyn_layer,"layer_list");
272 buildLayerList(user_layer,"user_layer_list");
273 } else {
274 dojo.connect(dyn_layer, "onLoad", function(evt){ buildLayerList(dyn_layer,"layer_list", "Basemap Dynamic")});
275 if (getUrlParam("app")) {
276 dojo.connect(user_layer, "onLoad", function(evt){ buildLayerList(user_layer,"user_layer_list",getUrlParam("app"))});
277 }
278 } 
when I run the code I always hit this condition
if (getUrlParam("app")) {
276 dojo.connect(user_layer, "onLoad", function(evt){ buildLayerList(user_layer,"user_layer_list",getUrlParam("app"))});
277 } 
in your buildlayerlist function you are looking for the visible layers and setting your check boxes according to that, you are also setting the visible layers back to the default values EFECTIVELY OVERRIDING your previous code which changed the visible layer to 3

So maybe in buildlayer list you want to check the "app" param and set your visible layer/checked layer according to what's currently visible in the mapservice.

I don't know the workflow for your site: but since the check boxes no longer appear in this layout, maybe when you check for "app" param, you don't have to worry about setting the check boxes and default layers at all.  Just some suggestions.

Let me know if this helps

P.S. the short url did not work when I tried it in IE, hence my first comment of not being redirected (IE6 and IE8)
0 Kudos
GlenRhea
Emerging Contributor
Thanks for your reply Harold and I see what you're talking about now. It makes sense that I don't need to build the layer list when the map is embedded. I have changed the code and it is working now, I should have seen that but I wrote the buildLayerList function awhile back and I didn't even think to look at it 😞

I'll update the code on the dev site so yall can check it out.
0 Kudos
GlenRhea
Emerging Contributor
Also, it's odd that the google url shortener didn't work in IE, here is the full path:

http://dev.geostor.arkansas.gov/g6/viewer.html?embed=true&h=750&w=750&app=app_districts&title=rep
0 Kudos