Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
share|improve this question
up vote 2 down vote accepted

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


share|improve this answer

Your Answer


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.