2

I am quite new to Javascript. I have made a html script that includes services I published to ArcGIS for server. It all does exactly what I want and I made it by copying working scripts together and getting help from more experienced scripters. I want to finetune my service by adding a legend. Unfortunately I am not able to create a working script. I have added the parts of my script I assume that are necessary to create my legend. Does anybody see what I am doing wrong?

     <script>
        var map;
        var wmsLayer;
        var URL;
        var legend;
        require(["dojo/query", "esri/map", "esri/layers/WMSLayer", "esri/dijit/Legend", "esri/config", "dojo/domReady!"],
        function (query, Map, WMSLayer, Legend, esriConfig) {

        esriConfig.defaults.io.proxyUrl = "/proxy/proxy_gcx.php";

          map = new Map("map", {
              basemap: "topo",
              center: [5.5, 52.485],
              minZoom: 8,
              maxZoom: 13,
              zoom: 8,
              slider: true
          });

          wmsLayer = new WMSLayer("URL", {
              format: "png",
              visibleLayers: ["31", "30", "29", "28"]
          });

          map.on("layer-add", function (results) {
              //add the legend
              legend = new Legend({
                  map: map,
                  layerInfos: [{
                      layer: results,
                      title: "test"
                  }]
              }, "legendDiv");
              legend.startup();
          });


          map.addLayer(wmsLayer);
      });
    </script>
</head>

<body>
    <div id="map">
    </div>
    <div id="legendDiv">
    </div>
</body>

EDIT: I have edited the code. I am still not done. It does give me a legend on my browser but it says there is no content in there. On my arcgis server manager there is a legend.

  • What version of ArcGIS Server are you running? – Mintx Mar 4 '15 at 23:00
  • I am running ArcGIS for Server 10.2.2 – Zuenie Mar 5 '15 at 8:10
  • Could the not showing of the legend have something to do with me adding a group layer in stead of a single layer? – Zuenie Mar 5 '15 at 8:29
2

When you're adding one layer, as you're doing, the map triggers the layer-add event. When adding a list of layers through map.addLayers(), it triggers the layers-add-result event. If you know you're going to be adding more layers, and want to use the map.addLayers() method to load them all at once, then you can do as Dan suggested:

map.addLayers([wmsLayer]);

If you're happy with the one layer you've loaded, then change your map event listener function to listen for layer-add.

map.on("layer-add", function (results) {
    //add the legend
    legend = new Legend({
        map: map,
        layerInfos: [{
            layer: wmsLayer,
            title: "test"
        }]
    }, "legendDiv");
    legend.startup();
});

EDIT: Oh yeah, and define the wmsLayer before you add the map.on("layer-add", ...) event listener.

  • You should probably change layer: wmsLayer to layer: results as "results" should be the layer that triggered the event. – Mintx Mar 4 '15 at 15:23
  • I am adding a single layer. So I am going to stick with the layer-add method. When I change the "layer: wmsLayer" to "layer: results" I dont call my layer anywhere in the legend. Perhaps I understand it wrong but it does not make sense to me. – Zuenie Mar 4 '15 at 15:51
  • the results value that is returned by the function is either the layer, or an object with the "layer" value containing the layer you added. If you added more layers through the addLayer() method, it would create a new legend on the legendDiv. If only want that wmsLayer to show in the legend, what you have is perfectly fine. If you used "layer: result" instead, the legend you see would be the last layer you added. – raykendo Mar 4 '15 at 16:40
  • @raykendo Good point on adding more than one layer. Not sure what the results of that would actually be. – Mintx Mar 4 '15 at 22:53
  • Thank you very much for all your help and insight. I guess I am still doing something wrong now but will have to able to figure it out myself. I have a location for a legend in which it says in Dutch: No legend. So I guess the linking towards my legend is wrong. – Zuenie Mar 5 '15 at 8:25
2

Since you are listening for the layers-add-result event, you need to add layers with the addLayers method (not addLayer).

So change this: map.addLayer(wmsLayer)

To this: map.addLayers([wmsLayer])

  • Thank you for the comment but it does not even load the layer in this way. Perhaps I should adapt the layers-add-results event? – Zuenie Mar 4 '15 at 14:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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