Select to view content in your preferred language

Is there a function to getLayerDefinitions (similar to setLayerDefinitions) from web?

1252
6
Jump to solution
06-16-2014 12:57 PM
CindyLu
Deactivated User
Hello,

Is there a function in Javascript APi layer.getLayerDefinitions from a web page map layers? I need to get this information using Javascript API. Or work around?

Thank you very much for any help.

Cindy
0 Kudos
1 Solution

Accepted Solutions
ManishkumarPatel
Deactivated User
Hi Cindy,

I created a jsfiddle for you to refer.
http://jsfiddle.net/patelmanya/7Vgv7/

Click the Get layer Definition button to get the layer defintion for the layer added to the map.

Hope this helps.

Regards,
Manish

View solution in original post

0 Kudos
6 Replies
OwenEarley
Frequent Contributor
Hi Cindy,

You can get this directly from a map service REST endpoint, for example:

http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/la...

To access this JSON data in your JS code, use an esri.Request:

[HTML]<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"/>
    <title>Map Layers - JSON</title>
    <script src="http://js.arcgis.com/3.9/"></script>
    <script>   
        require([
            "esri/request"
        ], function(esriRequest) {
            var layerUrl = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/la...";
            var layersRequest = esriRequest({
            url: layerUrl,
            content: { f: "json" },
            handleAs: "json",
            callbackParamName: "callback"
            });
            layersRequest.then(
            function(response) {
              console.log("Success: ", response.layers);
              // process map service layers
            }, function(error) {
              console.log("Error: ", error.message);
            });
        });
    </script>
  </head>
  <body>
    <p>See the console output for map service layers</p>   
  </body>
</html>[/HTML]

Hope this helps.

Owen
www.spatialxp.com.au
0 Kudos
ManishkumarPatel
Deactivated User
Hi Cindy,

You can try to use the below code to get the layer definition if it exists.

map.getLayer(map.layerIds[1]).layerDefinitions

or

map.getLayer("layerId").layerDefinitions

If  there is no layerDefinitions then the lenght will be 0.

Hope this helps.

Regards,
Manish
0 Kudos
CindyLu
Deactivated User
Hi Manish,

Thank you for your response. I tried layer.layerDefinitions, I got none.

I also want to mension that in the service, there is no layer definition. But I set the layer definition in Javascript code early. Then I want to get this layer definition for print.
===========

dojo.forEach(map.layerIds, function(layer, i1){
  var ml = map.getLayer(layer);
  switch (ml.declaredClass) {
   case "esri.layers.ArcGISDynamicMapServiceLayer":
    var s = dojo.forEach(ml.layerInfos, function(sublayer, i2){
     if (dojo.indexOf(ml.visibleLayers, sublayer.id.toString()) !== -1) {
      alert(sublayer.name);
      alert(sublayer.id);
      alert(sublayer.layerDefinitions);
      visiblelayers[sublayer.name] = true;
      layerQuery[sublayer.name] = sublayer.layerDefinitions;
     }
     else {
      visiblelayers[sublayer.name] = false;
     }
    });
===============
But sublayer.layerDefinitions gives Null.

Thank you very much for your help.

Cindy
0 Kudos
ManishkumarPatel
Deactivated User
Hi Cindy,

I created a jsfiddle for you to refer.
http://jsfiddle.net/patelmanya/7Vgv7/

Click the Get layer Definition button to get the layer defintion for the layer added to the map.

Hope this helps.

Regards,
Manish
0 Kudos
CindyLu
Deactivated User
Thanks. I got it (even though it is not so easy when I have more than one layer services, and each layer service has sublayers).

Cindy
0 Kudos
CindyLu
Deactivated User
Hi Owen,

Your method may work, even though I have not try your method yet.

Thanks,
Cindy
0 Kudos