[GIS] Get displaylevels/zoom level javascript API

arcgis-10.1arcgis-javascript-apiarcgis-online-basemaps

Is there any way using JavaScript API to get the current "displayLevels" of the basemap or "basemap zoom level" or "basemap scale" of the map and if it was more/less than certain number I change the Layer to another layer?

function(
        Map,
        FeatureLayer
        ){

map = new Map("map", {
           basemap: "topo",
           center: [-85.416, 49.781],
           zoom : 5,
           logo: false});
if (map zoom level == ***) {

 layer1 = new FeatureLayer(
                "http://testServer/arcgis/rest/services/test/MapServer/0", {
                        mode: FeatureLayer.MODE_ONDEMAND
                        });

        map.addLayer(layer1);
}

else {
layer2 = new FeatureLayer(
                "http://testServer/arcgis/rest/services/test/MapServer/1", {
                        mode: FeatureLayer.MODE_ONDEMAND
                        });

        map.addLayer(layer2);

}

Best Answer

Map has the property getLevel which you can use to find the zoom level.

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"/>
    <title>Simple Map</title>
    <link rel="stylesheet" href="http://js.arcgis.com/3.7/js/esri/css/esri.css">
    <style>
      html, body, #map {
        height: 100%;
        width: 100%;
        margin: 0;
        padding: 0;
      }
      body {
        background-color: #FFF;
        overflow: hidden;
        font-family: "Trebuchet MS";
      }
    </style>
    <script src="http://js.arcgis.com/3.7/"></script>
  <script>
    var map;

    require(["esri/map", "dojo/domReady!"], function(Map) {
        map = new Map("map", {
          basemap: "topo",
          center: [-122.45,37.75], // long, lat
          zoom: 13,
          sliderStyle: "small"
        });

        map.on("extent-change", function(){console.log(map.getLevel());})
      });
    </script>
  </head>

  <body>
    <div id="map"></div>
  </body>
</html>
Related Question