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?
Solved! Go to Solution.
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); }
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); }
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); }
you can use
map.removeLayer(map.getLayer(map.graphicsLayerIds[/*index of your graphical layer*/]));