[GIS] Change display name for feature layer when using LayerList widget

arcgis-javascript-api

I'm building an app with the ArcGIS JavaScript API, version 3.16. I have a map with two feature layers and I'd like to use the LayerList widget so the user can toggle them on and off. It's functional as I have it written, but not ideal in a couple of ways. The layers appear in the layer list as "graphicsLayer1" and "graphicsLayer2", and I'd like to find a way to give them more descriptive names. Also, the layers are visible when the box is unchecked, and become invisible when I check the box, and I'd like to flip that around.

The relevant section of code is below.

    var mapMain = new Map("mapDiv", {
        basemap: "topo",
        center: [-120, 38],
        zoom: 6
    });

    var resLyrLn = new FeatureLayer("https://data.farwestern.com/arcgis/rest/services/scratch/test_SD_RelatedResources/MapServer/0", {
        mode: FeatureLayer.MODE_ONDEMAND,
        visible: true,
    });
    var resLyrArea = new FeatureLayer("https://data.farwestern.com/arcgis/rest/services/scratch/test_SD_RelatedResources/MapServer/1", {
        mode: FeatureLayer.MODE_ONDEMAND,
        visible: true,
    });

    var lyrs = [resLyrArea, resLyrLn]

    mapMain.addLayers(lyrs);

    var layerListDijit = new LayerList({
        map: mapMain,
        layers: lyrs
    }, "layersDiv");
    layerListDijit.startup();

enter image description here

Best Answer

For the first question of renaming the layers, just add an "id" property when constructing the FeatureLayer:

var resLyrLn = new FeatureLayer("https://data.farwestern.com/arcgis/rest/services/scratch/test_SD_RelatedResources/MapServer/0", {
  mode: FeatureLayer.MODE_ONDEMAND,
  visible: true,
  id: "My Layer"});

The second question about checkbox layer control doesn't seem right. In my test, the layers were on when checked and off when unchecked. In your image, the layers are unchecked and I don't see any data. Could you elaborate or maybe give an updated image?

Related Question