Set scale for each sublayer in ArcGISDynamicMapServiceLayer

1632
2
Jump to solution
03-02-2017 12:54 PM
EvelynHernandez
Occasional Contributor III

Hello there,

Im trying to see how can i set the layer scale in a  ArcGISDynamicMapServiceLayer. I have a service with this kind of url

services/mycitycity/MapServer which contains 3 sublayers: cars, streets, people.

So i did this code for adding the layer to a map (im using es6)

var myLayer = new ArcGISDynamicMapServiceLayer(layers.read_dynamic_ap(),{});
myLayer .setImageFormat("png32");
 var layerDefinitions = [];
layerDefinitions[0] = "country= '"+ this.state.country[0].queryName+"'";
 layerDefinitions[1] = "country= '"+ this.state.country[0].queryName+"'";
 layerDefinitions[2] = "country= '"+ this.state.country[0].queryName+"'";
 layerDefinitions[4] = "country = '"+ this.state.country[0].queryName+"'";
myLayer .setLayerDefinitions(layerDefinitions);
 mapp.addLayer(myLayer );‍‍‍‍‍‍‍‍‍

So when i see the layers on the map, its ok, but i want to show them in a different scale every one due to one of them contains too much data, and when u see it from afar, i get this:

I dont know if i should separate each dynamic layer and add them using mapp.addLayers(dynLayer1,dynLayer2,dynLayer3) or how to set the scale separately.

Also i want to know how can i put the visibility on and off for each one in case i need to separate them.

Any example code should be great.

I have been looking this websites: 

ArcGIS API for JavaScript Sandbox : cuz the main reason im changing the layers to dynamic (i use featured) its about the identify element over there.

ArcGISDynamicMapServiceLayer | API Reference | ArcGIS API for JavaScript 3.19 

Thanks in advice!

0 Kudos
1 Solution

Accepted Solutions
EvelynHernandez
Occasional Contributor III

Well its something i was thinking yesterday.

I solved the problem already.

Here its what i did (could be useful for someone) (im using es6 and react):

1.- I set layer definitions for each sublayer on the Dynamic Service. (

var layerDefinitions = [];
 layerDefinitions[0] = "COUNTRY = '"+ this.state.COUNTRY [0].queryName+"'";
 layerDefinitions[1] = "COUNTRY = '"+ this.state.COUNTRY [0].queryName+"'";

2.- I set a variable per many sublayer i wanted to add to the map. In this case im going to show just 2, one with scale and one with the default scale (for seeing for afar). Also i set the layer defs for each one.

var carsLayer = new ArcGISDynamicMapServiceLayer(layers.read_dynamic_ap(),{ minScale: 6000});
carsLayer .setImageFormat("png32");
carsLayer .setVisibleLayers([0]);
carsLayer .setLayerDefinitions(layerDefinitions);

var streetsLayer = new ArcGISDynamicMapServiceLayer(layers.read_dynamic_ap(),{});
streetsLayer .setImageFormat("png32");
streetsLayer .setVisibleLayers([1]);
streetsLayer .setLayerDefinitions(layerDefinitions);‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

3.- I added them to the map in the secuence i want to show them (for example i want the streets goes first, so i put it at last)

mapp.addLayers([carsLayer , streetsLayer ]);
‍‍‍

4.- Then how i wanna save the layers in some variable to use the show() and hide() method for the layerlist i made, i just keep them in dynamicService.

this.setState({dynamicService: [carsLayer, streetsLayer]})‍‍‍‍‍

Then , for my custom layerList i made, i just change the visibility doing :

  this.state.dynamicService[0].show();  //the number is the position of the layer i saved and im going to use, this case is cars.‍‍‍‍‍‍

Thanks for all the help! I figure out how to solve my issue

View solution in original post

2 Replies
FilipKrál
Occasional Contributor III

Hi Evelyn,

If all your layers are included in a single service, you may want to set the scale range visibility of individual layers directly in the map document and republish the service.

In ArcGIS Desktop:

http://desktop.arcgis.com/en/arcmap/10.3/map/working-with-layers/displaying-layers-at-certain-map-sc...

In ArcGIS Online?

https://doc.arcgis.com/en/arcgis-online/create-maps/set-visibility.htm

When you then add that service to your map as a single layer, the layers should turn on and off as you zoom in and out.

If you don't have access to the map document, the server, or the map service in ArcGIS Online, you'll need to do it all in the JavaScript client. Define multiple layers, each displaying only a single layer from the service and set the minScale and maxScale properties as appropriate when you create the layers. Also the setScaleRange(minScale, maxScale) may be also useful. Try calling something like myLayer1.setScaleRange(100000, 5000); before you add it to the map.

Regardless of scale range visibility, call myLayer1.show(); and myLayer1.hide(); to turn myLayer1 on and off, respectively. Note that if the layer is not within it's visible scale range, nothing will be shown even if the layer is "on".

Hope this makes sense.

Filip.

EvelynHernandez
Occasional Contributor III

Well its something i was thinking yesterday.

I solved the problem already.

Here its what i did (could be useful for someone) (im using es6 and react):

1.- I set layer definitions for each sublayer on the Dynamic Service. (

var layerDefinitions = [];
 layerDefinitions[0] = "COUNTRY = '"+ this.state.COUNTRY [0].queryName+"'";
 layerDefinitions[1] = "COUNTRY = '"+ this.state.COUNTRY [0].queryName+"'";

2.- I set a variable per many sublayer i wanted to add to the map. In this case im going to show just 2, one with scale and one with the default scale (for seeing for afar). Also i set the layer defs for each one.

var carsLayer = new ArcGISDynamicMapServiceLayer(layers.read_dynamic_ap(),{ minScale: 6000});
carsLayer .setImageFormat("png32");
carsLayer .setVisibleLayers([0]);
carsLayer .setLayerDefinitions(layerDefinitions);

var streetsLayer = new ArcGISDynamicMapServiceLayer(layers.read_dynamic_ap(),{});
streetsLayer .setImageFormat("png32");
streetsLayer .setVisibleLayers([1]);
streetsLayer .setLayerDefinitions(layerDefinitions);‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

3.- I added them to the map in the secuence i want to show them (for example i want the streets goes first, so i put it at last)

mapp.addLayers([carsLayer , streetsLayer ]);
‍‍‍

4.- Then how i wanna save the layers in some variable to use the show() and hide() method for the layerlist i made, i just keep them in dynamicService.

this.setState({dynamicService: [carsLayer, streetsLayer]})‍‍‍‍‍

Then , for my custom layerList i made, i just change the visibility doing :

  this.state.dynamicService[0].show();  //the number is the position of the layer i saved and im going to use, this case is cars.‍‍‍‍‍‍

Thanks for all the help! I figure out how to solve my issue