0

Is it possible to use layer name instaed of layerid in identifyparams. However, over time, the layer order will change because I'll be adding new layers. Therefore, I want to refer to the layers by name, instead of id.can anyone suggest me please (am using arcgis api javascript api 3.17 example https://developers.arcgis.com/javascript/3/sandbox/sandbox.html?sample=find_popup)

2 Answers 2

1

Here's an example that returns the layerId of a named layer.

  var map, layerIds = [];

  require([
    "esri/map",
    "esri/layers/ArcGISDynamicMapServiceLayer",
    "dojo/_base/array"
  ], function (
    Map, ArcGISDynamicMapServiceLayer, array) {

    map = new Map("mapDiv", {
      sliderOrientation : "horizontal"
    });


    //Takes a URL to a non cached map service.
    var dynamicMapServiceLayer = new ArcGISDynamicMapServiceLayer("http://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer", {
    });

    map.addLayer(dynamicMapServiceLayer);

    map.on("load", function () {
        layerIds.push(getLayerIdFromMap(dynamicMapServiceLayer, "states"));
    });

    function getLayerIdFromMap(dynamicService, subLyrName) {
        var dLyr = map.getLayer(dynamicMapServiceLayer.id);
        var layerId;
        array.some(dLyr.layerInfos, function (lyrInfo) {
            if (lyrInfo.name === subLyrName) {
                layerId = lyrInfo.id;
            }
        }, this);
        return layerId;
    }
  });
2
  • thank u kenbuja.. here is my code am not getting where should i apply the above code can u suggest me on that Aug 19, 2016 at 4:57
  • 1
    I have made edited the code to show how to create a layerIds array that you can use in the line identifyParams.layerIds = layerIds; in your mapReady function. You can run the line `layerIds.push(getLayerIdFromMap(dynamicMapServiceLayer, "your layer name"));' multiple times for each layerId you want to be included in the IdentifyTask
    – kenbuja
    Aug 19, 2016 at 13:50
0
 require([
  "esri/map",
  "esri/InfoTemplate",
  "esri/layers/ArcGISDynamicMapServiceLayer",
  "esri/symbols/SimpleFillSymbol",
  "esri/symbols/SimpleLineSymbol",
  "esri/tasks/IdentifyTask",
  "esri/tasks/IdentifyParameters",
  "esri/dijit/Popup",
  "dojo/_base/array",
  "esri/Color",
  "dojo/dom-construct",
  "dojo/domReady!"
], function (
  Map, InfoTemplate, ArcGISDynamicMapServiceLayer, SimpleFillSymbol,
  SimpleLineSymbol, IdentifyTask, IdentifyParameters, Popup,
  arrayUtils, Color, domConstruct
) {

    var identifyTask, identifyParams;

    var popup = new Popup({
        fillSymbol: new SimpleFillSymbol(SimpleFillSymbol.STYLE_SOLID,
          new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID,
            new Color([255, 0, 0]), 2), new Color([255, 255, 0, 0.25]))
    }, domConstruct.create("div"));



    map.on("load", mapReady);

    var mapserviceURL = "http://117.247.187.3:6080/arcgis/rest/services/Local/LocalVarunaAdminV1/MapServer";

    map.addLayer(new ArcGISDynamicMapServiceLayer(mapserviceURL));

    function mapReady() {
        map.on("click", executeIdentifyTask);
        //create identify tasks and setup parameters
        identifyTask = new IdentifyTask(mapserviceURL);

        identifyParams = new IdentifyParameters();
        identifyParams.tolerance = 3;
        identifyParams.returnGeometry = true;
        //identifyParams.layerIds = [35, 0];
        identifyParams.layerOption = IdentifyParameters.LAYER_OPTION_ALL;
        identifyParams.width = map.width;
        identifyParams.height = map.height;
    }


function executeIdentifyTask(event) {
        identifyParams.geometry = event.mapPoint;
        identifyParams.mapExtent = map.extent;

        var deferred = identifyTask
          .execute(identifyParams)
          .addCallback(function (response) {
              // response is an array of identify result objects
              // Let's return an array of features.
              return arrayUtils.map(response, function (result) {
                  var feature = result.feature;
                  var layerName = result.layerName;

                  feature.attributes.layerName = layerName;
                  if (layerName === 'Assembly') {
                      var taxParcelTemplate = new InfoTemplate("Assembly Details", "AssemblyCode:${AssemblyCode}<br /> AssemblyName:${AssemblyName}");
                      feature.setInfoTemplate(taxParcelTemplate);
                  }
                  else if (layerName === 'District Hospital') {
                      console.log(feature.attributes.AssemblyCode);
                      var buildingFootprintTemplate = new InfoTemplate("District Hospital Details", "HealthFaci Name:${HealthFaci}<br /> CategoryNa:${CategoryNa}<br /> AssemblyCode :${AssemblyCode}");
                      feature.setInfoTemplate(buildingFootprintTemplate);
                  }
                  return feature;
              });
          });

this is my code here am adding layerids maually can u suggest me whre ti apply the above code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.