[GIS] Load One Layer from ArcGIS Server MapServer

arcgis-javascript-apiarcgis-serverjavascript

I am pretty new to ArcGIS Server and am building a map applications using the Javascript API. I can easily load the map layer below by adding it as a ArcGISDynamicMapServiceLayer

http://www.parkcounty.org/ArcGIS/rest/services/Roads_Map/MapServer/

As you can see there are many layers as part of the above url. What if I just want to load the roads layer as a ArcGISDynamicMapServiceLayer? I've tried replacing the above url with the road specific URL below and that does not seem to work

http://www.parkcounty.org/ArcGIS/rest/services/Roads_Map/MapServer//2

Is there something else I can do as the front end developer or is it up to those that are hosting and managing the server to create a unique url for the roads?

Best Answer

I know that this is an old question and already has an accepted answer. However, the answer goes over exporting the layer as an image. It doesn't seem to answer the OP's question. Because this is the first question that showed up when I was looking up this problem, I felt that I would add my solution here for others to try.

I first created a new ArcGISDynamicMapServiceLayer with the MapServer's URL, then used the .setVisibleLayers function to denote which layers in the MapServer should be displayed.

I can't use the OP's example MapServer because it has been taken down. But lets use esri's state, city, highway layer as an example. If I only want to show the highways on the map:

var newLayer = new ArcGISDynamicMapServiceLayer('http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StateCityHighway_USA/MapServer');
var layerIds = [0];
newLayer.setVisibleLayers(layerIds);
map.addLayer(newLayer);

If I wanted to show highways and states, then I would just replace the [0] with [0, 2].

Related Question