0

Can i create 2 legend objects in one script?

I'm currently working on a web-application in which i already do have a legend/TOC running and it looks like this:

var legendLayers=[];
 legendLayers.push({layer:raum, title:'Raumkategorien', slider:true});
 map.on("layers-add-result", function(){
 var layerInfo = new TOC({
 map: map,
 layerInfos: legendLayers,
 }, "legendDiv");
 layerInfo.startup();
 });
 map.addLayers([raum]);

Now I added some geoprocessing functionality and i wanted to add a new TOC when the result map is loaded in the web-app. I tried this by doing the following:

function gpJobComplete(jobinfo){
 var mapurl = mapServiceURL + "/" + jobinfo.jobId;
 var energy = new ArcGISDynamicMapServiceLayer(mapurl,{
 id:"Beste Energieformen",
 "opacity": 0.85
 });
 map.addLayers([energy]);
 var outputLayers = [];
 outputLayers.push({layer:energy, title:"Beste Energieformen"});
 map.on("layers-add-result", function(){
 domUtils.show(dom.byId('outputDiv'));
 var outputInfo = new TOC({
 map: map,
 layerInfos: outputLayers
 }, "outputDiv");
 outputInfo.startup();
 });
 }

I get a result map and it works just nice in the web app, but there's no new TOC. Am i trying it the wrong way?

asked Mar 31, 2014 at 18:17

2 Answers 2

1

Have you destroyed your previous TOC? I've found that the legend widget get's confused if you build a new one without first destroying the old one. The legend widget has a destroy method, I'm not sure if the TOC does as well. Try completely removing the old TOC before building a new one.

Good Luck.

It's essentially something like this. But as I said I'm just using the legend widget not the TOC widget.

 if (biosatApp.hasOwnProperty("legend")) {
 biosatApp.legend.destroy();
 domConstruct.destroy(dojo.byId("legendDiv"));
 }
 // create a new div for the legend
 var legendDiv = domConstruct.create("div", {
 id: "legendDiv"
 }, dom.byId("EILegendContainer"));
 biosatApp.legend = new Legend({
 map: biosatApp.map,
 layerInfos: biosatApp.legendLayers
 }, legendDiv);
 biosatApp.legend.startup();
answered Mar 31, 2014 at 18:45
6
  • @Simmal try destroying the TOC. I think layerInfos is the list of legend layers, but not the actual legend. Commented Mar 31, 2014 at 21:42
  • No, i destroyed layerInfo not layerInfos and layerInfo was the TOC (var layerInfo = new TOC). See the first script. I saw that you had some similar problem. Is it possible that i can have a look on your script and how it worked? Commented Apr 1, 2014 at 15:16
  • I just realised that the TOC won't show after running the geoprocessing task even though i uncommented the first (existing) TOC. Commented Apr 1, 2014 at 22:31
  • If you did a destroy it may have obliterated the entire div that it was in. You have to recreate the div to create a new legend/TOC in it. If you notice in the code above I have to create a div for the legend after destroying it. Commented Apr 2, 2014 at 19:14
  • Thanks. That obviously worked. I just had the problem that i defined my variable for the existing TOC in another funstion. I think that might have been the problem so that the second function didn't know about that variable. Commented Apr 4, 2014 at 14:55
0

so i tried destroying the existing toc and creating a new one like this, but it doesn't seem to work

function gpJobComplete(jobinfo){
 var mapurl = mapServiceURL + "/" + jobinfo.jobId;
 var energy = new ArcGISDynamicMapServiceLayer(mapurl,{
 id:"Beste Energieformen",
 "opacity": 0.85
 });
 layerInfo.destroy();
 var outputLayers = [];
 outputLayers.push({layer:energy, title:"Beste Energieformen"});
 map.on("layers-add-result", function(){
 var outputInfo = new TOC({
 map: map,
 layerInfos: outputLayers
 }, "outputDiv");
 outputInfo.startup();
 });
 map.addLayers([energy]);
 }
answered Mar 31, 2014 at 20:38

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.