AnsweredAssumed Answered

New Map Instance not loading base layer

Question asked by donnaghmcmahon181 on Sep 1, 2016

Hi Guys,

RE:(JavaScriptAPI)

I have a question to do with the ".removeAllLayers();" function. I am trying to remove all the layers on the current map except for the base layer (the map itself), however when i do "mainMap = new esriMap" it doesnt seem to work.

 

function BuildMap(layerMap) {

    esriUrlUtils.addProxyRule({

      urlPrefix: 1sttUrlPrefix, proxyUrl: urlProxy, });

   esriUrlUtils.addProxyRule({ urlPrefix: 2ndUrlPrefix, proxyUrl: urlProxy, });

 

esriConfig.defaults.io.corsDetection = false;

//Check if the map exists, if so - remove the existing layers //If not - just create a new map

if (mainMap == null) {

mainMap = new esriMap("map", { basemap: "satellite",

//Focus on the first location from the list (or the only location)

center: [modelLocations[0].Longitude, modelLocations[0].Latitude],

zoom: 6 });

}

else {

//Remove existing layers

removeLayers();

}

//Now, add new layers to the map  

addLayers(layerMap);

}

 

function removeLayers() {

                     //Initial code

//Remove all the existing layers apart from the base layer (the map itself)

//var mapLayersCount = mainMap.layerIds.length;

//if (mapLayersCount > 4) {

// for (var i = 0; i <= 3; i++) {

// mainMap.removeLayer(mainMap.getLayer(mainMap.layerIds[1]));

// };

//} else if (mapLayersCount > 3) {

// for (var i = 0; i <= 2; i++) {

// mainMap.removeLayer(mainMap.getLayer(mainMap.layerIds[1]));

// };

//} else if (mapLayersCount > 2) {

// for (var i = 0; i <= 1; i++) { /

/ mainMap.removeLayer(mainMap.getLayer(mainMap.layerIds[1]));

// };

//} else {

// mainMap.removeLayer(mainMap.getLayer(mainMap.layerIds[1]));

//}

tried several combinations to clear the map div and map instance.

mainMap.removeAllLayers();

mainMap.destroy();

mainMap = undefined;

$("#map").empty();

 

if (mainMap == null) {

mainMap = new esriMap("map", { basemap: "satellite",

//Focus on the first location from the list (or the only location)

center: [modelLocations[0].Longitude, modelLocations[0].Latitude],

zoom: 6 }); } }

Outcomes