setVisibleLayers to a function

2726
6
Jump to solution
01-26-2016 03:26 PM
JohnPreston
Occasional Contributor

Hi I dynamically create a listview based on data from SQL server, which lists maps layers and visibility. I can loop through list to create array or visible layers. This works great when I make a change in the listview but I would like to use the process to setVisibleLayers when the map is loaded. I am trying to set the setVisibleLayers method of layer to the same .get function I use elsewhere but the layers are not visible. I believe it is because listview has not been fully created yet. Is there a way I can set the visible layers this way?

var roadLayer = new esri.layers.ArcGISDynamicMapServiceLayer("http://gisappserv3.spokanecounty.org/ArcGIS/rest/services/Engineering/ENGINEERINGqueries_10/MapServe...",

{ id: "roadLayer",                    

visible: true                 });

roadLayer.setVisibleLayers([$('#ulLayerVisibility input[type="checkbox"]:checked').map(function () {                   

var checkid = $(this).attr('id').split(":");                    

return checkid[1];                

}).get()]);                

map = new Map("map", {                    

extent: customExtentAndSR               

});                

map.addLayers([roadLayer]);

0 Kudos
1 Solution

Accepted Solutions
JohnPreston
Occasional Contributor

Thank you Robert,

I went a slightly different way than what I did initially...I make a specific call to SQL for visible layers, instead of using dynamically built list. Then I set visible layers with results from SQL call.

var defaultVisible = [40, 51, 69, 65, 63, 64];

'$.ajax({

     type: "POST",

     url: myWebService,

     contentType: "application/json; charset=utf-8",

     dataType: "json",

     async: false,

     success: function (data) {

          var obj = jQuery.parseJSON(data.d);

          roadlayer.setVisibleLayer([obj]);

     },

     error: function (data) {

     'Unable to load Map Profile: ' + data.status + ' ' + data.statusText);

           roadlayer.setVisibleLayer([defaultVisible]);

     }

});

View solution in original post

0 Kudos
6 Replies
RobertScheitlin__GISP
MVP Emeritus

John,

   Not likely, The only way I can think that this would work is if you add the map object with not layers and then once the layer list is done getting the layer visibility from the database then have a function that will add the map layers.

I am really unsure of how you are attempting to use setVisibleLayers function here...

roadLayer.setVisibleLayers([$('#ulLayerVisibility input[type="checkbox"]:checked').map(function ()

setVisibleLayers function takes an array of layer ids.

0 Kudos
TracySchloss
Frequent Contributor

John,

I suggest a separate function to define an array of visibleLayers and then use that array as the input to setVisibleLayers.  What you have now makes no sense to me either.  Also, It seems like you want to start out with visibility:false and then not turn it back to true until you have your visibleLayers defined. 

0 Kudos
JohnPreston
Occasional Contributor

Thank you...

The ([$('#ulLayerVisibility input[type="checkbox"]:checked').map(function ()...returns an array of ids. It is a get function that return checked items from list. I use the same function to update visibility when user checks or unchecks an item in a list.

You are correct I need to get the layer list when it is done loading. That is my question...

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

John,

   With out seeing how you are getting the data from SQL Server (I can only assume you are using an esriRequest to a web service) it is hard to give a more define suggestion than I already have (i.e. get your layer vis request and then in the response function create the map object in code and add it the page). You do not have to have a map declared in html initially, you can add a map object programmatically.

0 Kudos
JohnPreston
Occasional Contributor

Thank you Robert,

I went a slightly different way than what I did initially...I make a specific call to SQL for visible layers, instead of using dynamically built list. Then I set visible layers with results from SQL call.

var defaultVisible = [40, 51, 69, 65, 63, 64];

'$.ajax({

     type: "POST",

     url: myWebService,

     contentType: "application/json; charset=utf-8",

     dataType: "json",

     async: false,

     success: function (data) {

          var obj = jQuery.parseJSON(data.d);

          roadlayer.setVisibleLayer([obj]);

     },

     error: function (data) {

     'Unable to load Map Profile: ' + data.status + ' ' + data.statusText);

           roadlayer.setVisibleLayer([defaultVisible]);

     }

});

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

John,

   Glad you got it working.

0 Kudos