In JS API 3.x we could get easily the scale for a box drawn on the map after you define its 4 corners.
In the matrix for the migration from 3.x to 4.x, it states that there are no plans for the scaleUtils to be migrated.
So, what's the best option to get the scale in 4.x?
var scale = esri.geometry.scaleUtils.getScaleForBox(
box,
map.getSpatialReference()
);
Solved! Go to Solution.
I haven't found any "getScaleForBox" function in the implementation of scaleUtils, however you can achieve the same result in 3.x via scaleUtils.getScale(map, extent, spatialReference), although the 2nd and 3rd parameters aren't documented.
As for 4.x, the implementation of the 3.x scaleUtils is fairly simple. Migrating the relevant part to 4.x yields something like the following:
const screenDPI = 96;
function _getUnitValue(spatialReference) {
if (spatialReference) {
var wkid, wkt;
if (typeof spatialReference == "object") {
wkid = spatialReference.wkid;
wkt = spatialReference.wkt;
} else if (typeof spatialReference == "number")
wkid = spatialReference;
else if (typeof spatialReference == "string")
wkt = spatialReference;
if ((typeof wkid == "number") && (wkid >= 0))
return WKIDUnitConversion.values[WKIDUnitConversion[wkid]];
else if ((typeof wkt == "string") && (wkt.search(/^PROJCS/i) !== -1)) {
var a = /UNIT\[([^\]]+)\]\]$/i.exec(wkt);
if ((a) && (a[1]))
return parseFloat(a[1].split(",")[1]);
}
}
return 0;
}
function getScale(mapView, extent, spatialReference) {
if ((mapView) && (mapView.width)) {
var scaleExtent = extent || mapView.extent;
if (scaleExtent) {
var srUnitValue = _getUnitValue(spatialReference || scaleExtent.spatialReference);
var unitValue = ((srUnitValue) ? srUnitValue : 6370997 * Math.PI / 180);
return scaleExtent.width / mapView.width * unitValue * 39.37 * screenDPI;
}
}
return 0;
}
As with 3.x, the 2nd and 3rd parameters are optional. Note that "WKIDUnitConversion" is esri/geometry/support/WKIDUnitConversion, which you'll need to include.
I haven't found any "getScaleForBox" function in the implementation of scaleUtils, however you can achieve the same result in 3.x via scaleUtils.getScale(map, extent, spatialReference), although the 2nd and 3rd parameters aren't documented.
As for 4.x, the implementation of the 3.x scaleUtils is fairly simple. Migrating the relevant part to 4.x yields something like the following:
const screenDPI = 96;
function _getUnitValue(spatialReference) {
if (spatialReference) {
var wkid, wkt;
if (typeof spatialReference == "object") {
wkid = spatialReference.wkid;
wkt = spatialReference.wkt;
} else if (typeof spatialReference == "number")
wkid = spatialReference;
else if (typeof spatialReference == "string")
wkt = spatialReference;
if ((typeof wkid == "number") && (wkid >= 0))
return WKIDUnitConversion.values[WKIDUnitConversion[wkid]];
else if ((typeof wkt == "string") && (wkt.search(/^PROJCS/i) !== -1)) {
var a = /UNIT\[([^\]]+)\]\]$/i.exec(wkt);
if ((a) && (a[1]))
return parseFloat(a[1].split(",")[1]);
}
}
return 0;
}
function getScale(mapView, extent, spatialReference) {
if ((mapView) && (mapView.width)) {
var scaleExtent = extent || mapView.extent;
if (scaleExtent) {
var srUnitValue = _getUnitValue(spatialReference || scaleExtent.spatialReference);
var unitValue = ((srUnitValue) ? srUnitValue : 6370997 * Math.PI / 180);
return scaleExtent.width / mapView.width * unitValue * 39.37 * screenDPI;
}
}
return 0;
}
As with 3.x, the 2nd and 3rd parameters are optional. Note that "WKIDUnitConversion" is esri/geometry/support/WKIDUnitConversion, which you'll need to include.