AnsweredAssumed Answered

Remove a map layer

Question asked by cromsos on Mar 5, 2015
Latest reply on Mar 6, 2015 by rscheitlin

Hi,

 

Last week the GeoNet community helped me get my fist web map application functioning!  Ive made a bit of progress since and find myself up against the limits of my javascript skill again.

 

I've got an app that calls a geoprocessing service and returns a map result.  It works as intended, once.  When I fire off a subsequent SubmitJob the new map layer is drawn underneath the existing layer (I presume it's drawn underneath because I don't see it).

 

So, seems that I could have the new layer draw on top using an index value but I'd rather just remove the layer and replace it with the new layer.

 

The function to add the layer looks like this:

// On Job Complete Callback add a dynamic map service using ResultMapService 

    function onTaskComplete(jobInfo, n){ 

   

        //replace mapservice url with your url and append jobid 

        var mapurl = "http://bhc.coas.oregonstate.edu:6080/arcgis/rest/services/geoprocessing/WeightedSum/MapServer/jobs/" + jobInfo.jobId;   

       

        //create a dynamic map service 

        var gpResultLayer = new esri.layers.ArcGISDynamicMapServiceLayer(mapurl, { 

        id: "WeightedSum", 

        opacity: 0.5 

        }); 

       

        //remove any existing model results and add new model result to the web application. 

        map.addLayer(gpResultLayer, n);

    }

I think I'd like to "remember" or return the gpResultLayer from this function such that the next time it executes I can add a map.removeLayer(gpResultLayerPrevious) before adding the new layer.

 

I tried adding:

 

var gpResultLayerPrevious = gpResultLayer

and

map.removeLayer(gpResultLayerPrevious)

to the above function but I think I need to make sure it gets returned from the function then passed in each time.  Any suggestions to how this might work?

 

Thanks, Chris

Outcomes