I am very new to the ArcGIS API for Javascript and am by no means a programmer. Nonetheless, I have been fairly successful at finding existing code and modifying it to my needs. That is, until now.
I want something that I think should be simple and straightforward. However, despite looking high and low, I cannot find a good code sample. I would like a button that, when clicked, shows or hides a legend (or TOC). Indeed, I have seen multiple examples of this in WebAppBuilder applications and elsewhere, but it has not been easy to tease through the code. Surely, some others have done this.
It seems to me that my answer should be in "dijit/form/DropDownButton." However, this newbie cannot seem to get it to work. My guess is I am overlooking the obvious (or, perhaps, I am approaching it all wrong).
I have attached a snippet of the code sample below.
require([
"esri/map",
"esri/layers/FeatureLayer",
"esri/InfoTemplate",
"esri/renderers/UniqueValueRenderer",
"esri/symbols/SimpleMarkerSymbol",
"esri/Color",
"esri/dijit/Legend",
"dijit/form/DropDownButton",
"dojo/_base/array",
"dojo/domReady!"
], function(Map, FeatureLayer, InfoTemplate, UniqueValueRenderer, SimpleMarkerSymbol, Color, Legend, DropDownButton, arrayUtils) {
map = new Map("map", {
basemap:"gray",
center: [-98.435731, 35.222876],
zoom: 7,
logo: false,
slider: false
});
var layer = new FeatureLayer("//services.arcgis.com/V6ZHFr6zdgNZuVG0/ArcGIS/rest/services/NationalParkStats2013/FeatureServer/0");
var renderer = new UniqueValueRenderer(null, "Type");
var symbol1 = new SimpleMarkerSymbol();
symbol1.setColor(new Color("#ed5151"));
renderer.addValue("National Park", symbol1);
var symbol2 = new SimpleMarkerSymbol();
symbol2.setColor(new Color("#149ece"));
renderer.addValue("National Monument", symbol2);
layer.setRenderer(renderer);
map.addLayers([layer]);
//add the legend
map.on("layers-add-result", function (evt) {
var layerInfo = arrayUtils.map(evt.layers, function (layer, index) {
return {layer:layer.layer, title:layer.layer.name};
});
if (layerInfo.length > 0) {
var legend = new Legend({
map: map,
layerInfos: layerInfo
}, "legend");
legend.startup();
}
});
});
My HTML body then has these divs:
<div id="mainWindow" data-dojo-type="dijit.layout.BorderContainer" data-dojo-props="design:'headline',gutters:false" style="width: 100%; height: 100%; margin: 0;">
<div id="header" data-dojo-type="dijit.layout.ContentPane" data-dojo-props="region:'top'">It certainly would be nice if this worked!
<div id="subheader">No really</div>
<!-- Legend widget inside a Drop Down Button -->
<div id="legend-wrapper">
<div data-dojo-type="dijit.form.DropDownButton">
<span>Legend</span>
<div data-dojo-type="dijit.TooltipDialog">
<div id="legend" dojoType="dijit.layout.ContentPane" style="overflow:auto;width:225px;height:100px;vertical-align:top;"></div>
</div>
</div>
</div>
</div>
<div id="map" class="shadow" data-dojo-type="dijit.layout.ContentPane" data-dojo-props="region:'center'"></div>
</div>