1

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

1 Answer 1

2

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?

2
  • I think the second question should be asked as a new question.
    – PolyGeo
    Commented Jun 9, 2016 at 21:47
  • Your id suggestion worked. Thanks very much, Mintx. As to the other, I deliberately zoomed out past the scale dependency for the screen shot because it's sensitive data that I can't go posting pictures of on the internet. Sorry about that. But I found that if I set the visibility for each layers, it fixes the issue: var layerListDijit = new LayerList({ map: mapMain, layers: [ { layer: resLyrLn, visibility: true }, { layer: resLyrArea, visibility: true } ] }, "layersDiv"); layerListDijit.startup();
    – geobug
    Commented Jun 9, 2016 at 22:02

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.