Select to view content in your preferred language

Having Issue On Removing Layer on ArcGIS API For JavaScript

3054
3
Jump to solution
09-15-2015 06:47 PM
BruceGreen
Occasional Contributor

I am adding a markers layer called `layer1` like this to map

 function drawPoints(mapInfo) {
      layer1 = new esri.layers.GraphicsLayer();
        for (var i = 0; i < mapInfo.length; i++) {
               var projects = mapInfo;
               var project = new esri.geometry.Point(projects.Longitude, projects.Latitude);
               project = esri.geometry.geographicToWebMercator(project);
               var symbol = new esri.symbol.PictureMarkerSymbol("img/map/marker.png", 18, 18);
               projectInfoTemplate = new InfoTemplate();
               projectInfoTemplate.setTitle("Project Details");
               projectInfoTemplate.setContent('<div class="row"></div> ');
               var projectsG = new esri.Graphic(project, symbol).setInfoTemplate(projectInfoTemplate);
               layer1.add(projectsG);
               }
              map.addLayer(layer1);
     }

now in next request I need to clear map so I used the

    map.removeLayer(layer1);

but this is causing error because the `layer1` still not created at first request. Now I need to check `IF` the map has a layer called `layer1` then removeit. Here is a pseudo code of what I need to do:

    if(map.has/contains/include(layer1){
      map.removeLayer(layer1);
    }

can you please let me know how to do that?

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
TyroneBiggums
Deactivated User

Make sure that your business rules and conditions are referencing the same data. If you are checking for map.getLayer by id then removing an instance of an object, you might get unexpected results.

Try:

var layer = map.getLayer(id);

if (layer) { map.removeLayer(layer); }

View solution in original post

3 Replies
RobertScheitlin__GISP
MVP Emeritus

Behrouz,

  Just do this (line 3 and 18 thru 20):

function drawPoints(mapInfo) { 
  layer1 = new esri.layers.GraphicsLayer();
  layer1.id = "MyGL";
  for (var i = 0; i < mapInfo.length; i++) { 
    var projects = mapInfo; 
    var project = new esri.geometry.Point(projects.Longitude, projects.Latitude); 
    project = esri.geometry.geographicToWebMercator(project); 
    var symbol = new esri.symbol.PictureMarkerSymbol("img/map/marker.png", 18, 18); 
    projectInfoTemplate = new InfoTemplate(); 
    projectInfoTemplate.setTitle("Project Details"); 
    projectInfoTemplate.setContent('<div class="row"></div> '); 
    var projectsG = new esri.Graphic(project, symbol).setInfoTemplate(projectInfoTemplate); 
    layer1.add(projectsG); 
  } 
  map.addLayer(layer1); 
}

if(map.getLayer("MyGL")){ 
  map.removeLayer(layer1); 
}
0 Kudos
TyroneBiggums
Deactivated User

Make sure that your business rules and conditions are referencing the same data. If you are checking for map.getLayer by id then removing an instance of an object, you might get unexpected results.

Try:

var layer = map.getLayer(id);

if (layer) { map.removeLayer(layer); }

RakeshModi
Deactivated User

you can use

map.removeLayer(map.getLayer(map.graphicsLayerIds[/*index of your graphical layer*/]));

0 Kudos