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[i];
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?
-
A little confused about your request, sounds like you are adding a layer and then immediately removing it automatically. Something else?timlohnes– timlohnes2015年09月16日 16:17:47 +00:00Commented Sep 16, 2015 at 16:17
1 Answer 1
You have to make sure it is loaded. I couldn't get layer1 to get recognized when I used 'load', but this works fine. Just click on map and the points will go away.
map.on("click", function unloadPoints() {
map.removeLayer(layer1);
});
I thought this would work, but like I said it didn't see layer1. So explore along these lines.
layer1.on("load", function unloadPoints() {
map.removeLayer(layer1);
});
Read more about events here https://developers.arcgis.com/javascript/jshelp/inside_events.html