show numeric scale for ArcGISDynamicMapServiceLayer?

706
2
11-21-2011 11:58 AM
SteveCole
Frequent Contributor
I'd like to display a numeric scale value while using a ArcGISDynamicMapServiceLayer but the only code examples I've been able to find so far relate to using an ArcGISTiledMapServiceLayer in which the LOD has a scale property.

Can this be done with ArcGISDynamicMapServiceLayers and does anyone have an example they can share?

Thanks!
Steve
0 Kudos
2 Replies
JianHuang
Occasional Contributor III
Steve,

It depends on the spatial reference, DPI and other factors. The easiest way probably is to add a scalebar on your map and exact the scale value from it:
var scalebarLengthInPixel = scalebar.domNode.style.width; // For example, you may get "112px" as returned value
var scalebarLengthInReality = dojo.query(".esriScalebarSecondNumber", scalebar.domNode)[0].innerHTML; //you may get "300mi" as returned value

Then, parse both values to numbers and calculate the scale value. For example, scalevalue = 300*1609*39.3700787*96/112  (1 mi = 1609 meters, 1 meter = 39.3700787 inches, and assuming the client DPI is 96)
If you don't like a visible scalebar, you can always call
esri.hide(scalebar.domNode); to hide it.
Hope this helps.
0 Kudos
JeffMitzelfelt
New Contributor III
I calculate my scale on an onExtentChange event using the XY dimensions of the map object (returned by the onExtentChange event) and the size of the mapDiv.





dojo.connect(map, "onExtentChange", updateExtent);
dojo.connect(window, 'resize', mapResize); 

pixelPerMeter = 96/.0254; // Define pixel per meter value (96 pixels per inch)

function mapResize(){
 var resizeTimer;
 clearTimeout(resizeTimer);
 resizeTimer = setTimeout(function(){
  sizeMap();
  map.resize();
  map.reposition();
  }, 500);
}

function sizeMap(){
 mapWidthPx = window.innerWidth;
 // Using 57 pixels for control/information bars
 mapHeightPx = window.innerHeight - 57;
 // Set calculated height for mapDiv
 $("#map").css({"height" : (mapHeightPx + "px")});
}

function updateExtent(evt){
 mapScale = calcScale(evt.xmax, evt.xmin, evt.ymax, evt.ymin);
 displayMapScale(mapScale); // Write map scale out to 
}

function calcScale(maxX, minX, maxY, minY) {
 var meterX = Math.abs(maxX - minX);
 var meterY = Math.abs(maxY - minY);
 var scaleX = meterX / mapWidthPx * pixelPerMeter;
 var scaleY = meterY / mapHeightPx * pixelPerMeter;
 var scaleMin = Math.round((scaleY>scaleX)?scaleX:scaleY);
  return scaleMin;
}  
0 Kudos