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);

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


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:


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 Sep 16 '15 at 16:17

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() {

I thought this would work, but like I said it didn't see layer1. So explore along these lines.

    layer1.on("load", function unloadPoints() {

Read more about events here https://developers.arcgis.com/javascript/jshelp/inside_events.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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