Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.graphics.clear();
    map.removeLayer("finalLayer"); //finalLayer is the id that I assigned to the featureLayer that I am adding

    var pointSymbol = new esri.symbol.SimpleMarkerSymbol();
    pointSymbol.setSize(20);
    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);
    map.graphics.add(graphic);

    var features = [];
    features.push(graphic);
    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
    });
    map.addLayers([featureLayer]);
}
share|improve this question

1 Answer 1

up vote 2 down vote accepted

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

map.removeLayer(map.getLayer(map.graphicsLayerIds[0]));

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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