[GIS] ESRI Maps – Refresh the map

arcgis-javascript-api

First time, map loads and the locations are plotted properly.

Second time, when the new set of locations are pulled from the app, map reloads and gets the new set of data. But it is not refreshed. The old plotted points are not getting removed.

Best Answer

For the JS API you will need to remove the old layer before (or after if you want...) you add the new one in.

For example:

map = new esri.Map('map')
lyr = new esri.layers.FeatureLayer('http://someurl')
map.addLayer(lyr)
newLyr = new esri.layers.FeatureLayer('http://someOtherURL')
map.removeLayer(lyr)
map.addLayer(newLyr)

The map should update on its own. I will often do map.removeAllLayers() and re-add my layers from scratch in the correct order because it simplifies generalization (and re-draw is almost not noticeable).