I was trying to create a FeatureLayer from the features returned from the ViewShed GP and I was successful at doing it. So when I click on the map, that featureSet is sent to the GP Tool and the return has all the features and I add it to the map as a FeatureLayer. Now I want to remove the layer when the user clicks again on the map, but strangely, the map.layerIds returns, ["layer0"], indicating only the basemap.

I am not sure how to remove the featureLayer? This is my code,

function init() {
    dojo.connect(map, "onClick", computeViewShed);

function computeViewShed(evt) {
    map.removeLayer("finalLayer"); //finalLayer is the id that I assigned to the featureLayer that I am adding

    var pointSymbol = new esri.symbol.SimpleMarkerSymbol();
    pointSymbol.setOutline(new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([0, 0, 0]), 1));
    pointSymbol.setColor(new dojo.Color([0, 0, 255, 0.25]));

    var graphic = new esri.Graphic(evt.mapPoint, pointSymbol);

    var features = [];
    var featureSet = new esri.tasks.FeatureSet();
    featureSet.features = features;
    var vsDistance = new esri.tasks.LinearUnit();
    vsDistance.distance = 5;
    vsDistance.units = "esriMiles";
    var params = {
        "Input_Observation_Point": featureSet,
        "Viewshed_Distance": vsDistance
    gp.execute(params, drawViewshed);

function drawViewshed(results, messages) {
    var polySymbol = new esri.symbol.SimpleFillSymbol();
    polySymbol.setOutline(new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([0, 0, 0, 0.5]), 1));
    polySymbol.setColor(new dojo.Color([255, 127, 0, 0.7]));
    var features = results[0].value.features;
    var layerDefinition = {
        "geometryType": "esriGeometryPolygon",
        "fields": [{
            "name": "OBJECTID",
            "alias": "ID",
            "type": "esriFieldTypeOID"
        }, {
            "name": "grid_code",
            "alias": "Grid Number",
            "type": "esriFieldTypeString"
        }, {
            "name": "Shape_Area",
            "alias": "Area",
            "type": "esriFieldTypeNumber"

    var featureCollection = {
        "layerDefinition": layerDefinition,
        "featureSet": {
            "features": features,
            "geometryType": "esriGeometryPolygon"
    //create a feature layer based on the feature collection
    featureLayer = new esri.layers.FeatureLayer(featureCollection, {
        id: 'finalLayer',
        mode: esri.layers.FeatureLayer.MODE_SNAPSHOT

1 Answer 1


It was simple just needed to fetch the id of the grpahics layer and add it to getLayer.


Your Answer

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

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