After adding ArcGISDynamicMapServiceLayer, zoom level of the basemap doesn't work

4480
8
10-10-2013 09:32 PM
jerryli
New Contributor
I add a ArcGISDynamicMapServiceLayer to the basemap, then when I am calling basemap.getZoom(), it always returns -1.
And the basemap.setZoom also doesn't work properly.
0 Kudos
8 Replies
ManishkumarPatel
Occasional Contributor II
I add a ArcGISDynamicMapServiceLayer to the basemap, then when I am calling basemap.getZoom(), it always returns -1.
And the basemap.setZoom also doesn't work properly.


Hi Jerry,

Not sure what you are trying to do. Are you trying to get the map zoom level. You can have a look at the API documentation for the available methods.

https://developers.arcgis.com/en/javascript/jsapi/map-amd.html#getzoom

map.getZoom(); //this will return the current zoom level of the map. A value of -1 indicates that the map does not have pre-defined zoom levels.

for setting the zoom level, try

map.setZoom(zoom); //provide the level you want to set.

Also if possible could you provide the code through jsfiddle so we can look into the details and give a faster resolution.

Thanks & regards,
Manish
0 Kudos
jerryli
New Contributor
Thanks Manish,

For example, I add a map with options
    var option = {
      // ESRI map options can be used still. For example, do not display "esri" logo.
      logo : false,
      center : [-74, 40.72],
      maxZoom : 10,
      minZoom : 1,
      zoom : 3
    };
    
    var map = ... //create a esri map.
    
    //then add layer to the map.
    var layerConfig  = {
      url : "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Population_World/MapServer",
      options : {
        "opacity" : 0.5
    };
    var layer = new esri.layers.ArcGISDynamicMapServiceLayer(layerConfig.url,
              layerConfig.options);
    map.addLayer(layer);
    
    //after the map is rendered and shown, then I call the get zoom function
    var level = map.getZoom();
    //the zoom level here is always -1, What I have set in the basemap is lost.
    //and map.setZoom is also changed, although I can use map.setZoom(0) to zoom in and map.setZoom(1) to zoom out, but this
    //kind of usage is not mentioned in the API

0 Kudos
ManishkumarPatel
Occasional Contributor II
Thanks Manish,

For example, I add a map with options
    var option = {
      // ESRI map options can be used still. For example, do not display "esri" logo.
      logo : false,
      center : [-74, 40.72],
      maxZoom : 10,
      minZoom : 1,
      zoom : 3
    };
    
    var map = ... //create a esri map.
    
    //then add layer to the map.
    var layerConfig  = {
      url : "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Population_World/MapServer",
      options : {
        "opacity" : 0.5
    };
    var layer = new esri.layers.ArcGISDynamicMapServiceLayer(layerConfig.url,
              layerConfig.options);
    map.addLayer(layer);
    
    //after the map is rendered and shown, then I call the get zoom function
    var level = map.getZoom();
    //the zoom level here is always -1, What I have set in the basemap is lost.
    //and map.setZoom is also changed, although I can use map.setZoom(0) to zoom in and map.setZoom(1) to zoom out, but this
    //kind of usage is not mentioned in the API



Hi Jerry,

You can try to use either of the below line of code to get the zoom level:

map.on("onLayerAddResult",getZoomLevel);
   or
dojo.connect(map,"onLayerAddResult",getZoomLevel);

function getZoomLevel(){
var zoomlvl = map.getZoom();
}

for your reference:
https://developers.arcgis.com/en/javascript/jsapi/map-amd.html#addlayers

Initially the code might be returning -1 because adding layers event might have not be completed.

Hope this helps.

Regards,
Manish
0 Kudos
jerryli
New Contributor
Hi Manish,

Sorry for a mistake, actually we call the map.getZoom() asynchronously. And this issue is still there. And this issue only occurs when we add a ArcGISDynamicMapServiceLayer. If we add some other layer else(IE. ArcGISTiledMapServiceLayer), there is no such issue.
0 Kudos
ManishkumarPatel
Occasional Contributor II
Hi Jerry,

Please take a look at this : http://jsfiddle.net/patelmanya/3FUbk/

let me know the results.

Thanks & regards,
Manish
0 Kudos
jerryli
New Contributor
Hi Jerry,

Please take a look at this : http://jsfiddle.net/patelmanya/3FUbk/

let me know the results.

Thanks & regards,
Manish


Hi Manish,

Thanks a lot. I get the reason, in the example given by you, the basemap is rendered. But in our code we don't give the basemap property so no basemap rendered and then what I said happens.

Please look at below link:

http://jsfiddle.net/3FUbk/12/
0 Kudos
ManishkumarPatel
Occasional Contributor II
Hi Manish,

Thanks a lot. I get the reason, in the example given by you, the basemap is rendered. But in our code we don't give the basemap property so no basemap rendered and then what I said happens.

Please look at below link:

http://jsfiddle.net/3FUbk/12/


Hi Jerry,

I looked at the code and what I can explain is that since you do not have any basemap set to your map it will always return -1 as zoom. Because the map.getZoom() method uses the LOD and returns the current map level.

Scenario 1:
You can debug and check if you have a basemap set and then you add your dynamic layer, the value for getZoom() will be always the current map level.

in other words, the LOD is set in the map using the tiling scheme from the basemap that's set.

(in debugging mode you can check this will always contain values)      map.__LOD

Scenario 2:

If you do not add the base map and add a Tiled map service layer. the map's LOD is set using the LOD from the Tiled Map Service layer.

map._LOD will consist values.

Scenario 3: (This is what you have)

If you do not set basemap and just add a Dynamic map service layer to the map then the map doesnt have any LOD, hence no level is returned when using getZoom().

if you check while debugging

map.__LOD = undefined

Hence it can be concluded that the getZoom() will return values if there is a basemap or  Tiled Map Service layer added to the map. Only having dynamic map service layer will always return -1.

Hope this helps.

Thanks & Regards,
Manish
0 Kudos
jerryli
New Contributor
Hi Manish,

Get it. I'd better to add the basemap along with the Dynamic map service layer. Thank you.:)
0 Kudos