0

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?

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Sep 16, 2015 at 1:43
1
  • A little confused about your request, sounds like you are adding a layer and then immediately removing it automatically. Something else? Commented Sep 16, 2015 at 16:17

1 Answer 1

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

answered Sep 16, 2015 at 16:12

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.