zaguayo

Legend on web map, scripting error

Discussion created by zaguayo on Mar 19, 2013
Hello,

I'm working with Arc's API for a school project. I have put in a few widgets into my script. The only one I am having issues with is the legend/Toggle, I am unable to get the toggle to work. Can someone take a look at my script and let me know what I'm doing wrong. I've been at this for about a week and I just don't know what I'm doing wrong. I followed all the directions. Thank you,

var map, legendLayers = [];

function init() {
map = new esri.Map("map", {
basemap: "topo",
center: [-110.934448, 32.331238],
zoom: 10
});

//Legend starts here
var homicideLayer = new esri.layers.ArcGISDynamicMapServiceLayer("http://masgis2.asu.edu:6080/arcgis/rest/services/Group4/Riggs_Test/MapServer/0",{id:'Homicide'});

legendLayers.push({layer:homicideLayer,title:'Homicide'});

var rapeLayer = new esri.layers.ArcGISDynamicMapServiceLayer("http://masgis2.asu.edu:6080/arcgis/rest/services/Group4/Riggs_Test/MapServer/1",{id:'Rape'});

legendLayers.push({layer:rapeLayer,title:'Rape'});

var aggassaultLayer = new esri.layers.ArcGISDynamicMapServiceLayer("http://masgis2.asu.edu:6080/arcgis/rest/services/Group4/Riggs_Test/MapServer/3",{id:'Aggravated Assault'});

legendLayers.push({layer:aggassaultLayer,title:'Aggravated Assault'});

var robberyLayer = new esri.layers.ArcGISDynamicMapServiceLayer("http://masgis2.asu.edu:6080/arcgis/rest/services/Group4/Riggs_Test/MapServer/2",{id:'Robbery'});

legendLayers.push({layer:robberyLayer,title:'Robbery'});


dojo.connect(map,'onLayersAddResult',function(results){
var legend = new esri.dijit.Legend({
map:map,
layerInfos:legendLayers
},"legendDiv");
legend.startup();
});
map.addLayers([homicideLayer,rapeLayer,aggassaultLayer,robberyLayer]);

dojo.connect(map,'onLayersAddResult',function(results){

//add check boxes
dojo.forEach(legendLayers,function(layer){
var layerName = layer.title;
var checkBox = new dijit.form.CheckBox({
name: "checkBox" + layer.layer.id,
value: layer.layer.id,
checked: layer.layer.visible,
onChange: function(evt) {
var clayer = map.getLayer(this.value);
clayer.setVisibility(!clayer.visible);
this.checked = clayer.visible;
}
});

//add the check box and label to the toc
dojo.place(checkBox.domNode,dojo.byId("toggle"),"after");
var checkLabel = dojo.create('label',{'for':checkBox.name, innerHTML:layerName},checkBox.domNode,"after");
dojo.place("<br />",checkLabel,"after");
});
});

Outcomes