How do I change the Scalebar Units between feet and miles depending on the Map scale

519
3
Jump to solution
09-08-2022 06:44 PM
RichardFairhurst
MVP Honored Contributor

I have an alternating scalebar in my layout.  In my Addin the user selects features they want to locate on the map and zooms to their selection.  As long as the map scale required to display all of the features is not zoomed out beyond 1:4000 feet I want the scalebar units to be displayed in Feet and adjust the map scale to make sense for feet.  If the map has to zoom out beyond 1:4000 feet to display all of the features I adjust the scale to the nearest 1/2 mile interval that contains all of the features and want the scalebar units to be displayed in Miles.  I want to programmatically change the UnitLabel and Units of the scalebar to either feet or miles depending on the scale of the map.  I have figured out hoe to access Scalebar's UnitLabel and Units properties.  The UnitLabel is set as a string, but the Units needs to be set as an ArcGIS.Core.Geometry.Unit.  When I get the Units from the Scalebar as a ArcGIS.Core.Geometry.Unit variable all of its properties that I want to change are read only.  How do I set up an instance of an ArcGIS.Core.Geometry.Unit for feet and for Miles so I can set the Scalebar Units property to match the unit rules I am applying to the map scale my Addin is setting?

Here is code I have so far:

ScaleBar alternatingScaleBar = lyt.FindElement("Alternating Scale Bar") as ScaleBar;
                            if (alternatingScaleBar != null)
                            {
                                CIMDoubleFillScaleBar dfScaleBar = alternatingScaleBar.GetDefinition() as CIMDoubleFillScaleBar;
                                dfScaleBar.UnitLabel = "Feet";
                                // I can get the current unit setting of
                                // the scalebar with this code, but I can't
                                // change the unit setting of this variable
                                // and I don't know how to set up my own
                                // ArcGIS.Core.Geometry.Unit variable that
                                // I can use to change the Units property.
                                var sbUnits = dfScaleBar.Units;
                            }
Tags (1)
0 Kudos
1 Solution

Accepted Solutions
RichardFairhurst
MVP Honored Contributor

I figured out the answer.  Here is the code I came up with:

 

 

var ActMap = MapView.Active;
MapView mapView = ActMap;
Camera camera = mapView.Camera;
double scaleFt = camera.Scale/12; // Convert inches to feet
ScaleBar alternatingScaleBar = lyt.FindElement("Alternating Scale Bar") as ScaleBar;
if (alternatingScaleBar != null)
{
    CIMDoubleFillScaleBar scaleBarDef = alternatingScaleBar.GetDefinition() as CIMDoubleFillScaleBar;
    if (scaleFt <= 4000)
    {
        scaleBarDef.UnitLabel = "Ft";
        string unitJsonFt = "{\"uwkid\":9002}";
        var unitFt = ArcGIS.Core.Geometry.Unit.CreateFromJson(unitJsonFt);
        scaleBarDef.Units = unitFt;
    }
    else
    {
        scaleBarDef.UnitLabel = "Mi";
        string unitJsonMi = "{\"uwkid\":9093}";
        var unitMi = ArcGIS.Core.Geometry.Unit.CreateFromJson(unitJsonMi);
        scaleBarDef.Units = unitMi;
    }
    alternatingScaleBar.SetDefinition(scaleBarDef);
}

 

 

View solution in original post

3 Replies
RichardFairhurst
MVP Honored Contributor

I figured out the answer.  Here is the code I came up with:

 

 

var ActMap = MapView.Active;
MapView mapView = ActMap;
Camera camera = mapView.Camera;
double scaleFt = camera.Scale/12; // Convert inches to feet
ScaleBar alternatingScaleBar = lyt.FindElement("Alternating Scale Bar") as ScaleBar;
if (alternatingScaleBar != null)
{
    CIMDoubleFillScaleBar scaleBarDef = alternatingScaleBar.GetDefinition() as CIMDoubleFillScaleBar;
    if (scaleFt <= 4000)
    {
        scaleBarDef.UnitLabel = "Ft";
        string unitJsonFt = "{\"uwkid\":9002}";
        var unitFt = ArcGIS.Core.Geometry.Unit.CreateFromJson(unitJsonFt);
        scaleBarDef.Units = unitFt;
    }
    else
    {
        scaleBarDef.UnitLabel = "Mi";
        string unitJsonMi = "{\"uwkid\":9093}";
        var unitMi = ArcGIS.Core.Geometry.Unit.CreateFromJson(unitJsonMi);
        scaleBarDef.Units = unitMi;
    }
    alternatingScaleBar.SetDefinition(scaleBarDef);
}

 

 

HelenZhou
Occasional Contributor II

Hi @RichardFairhurst , how is the json constructed? "{\"uwkid\":9002}"  ?

It is related with spatial reference wkid or is  this just for generic feet? where do I find the reference to construct the json for feet unit in spatial reference like 2276? Thanks

0 Kudos
HelenZhou
Occasional Contributor II

I have found this documentation -https://pro.arcgis.com/en/pro-app/latest/help/mapping/properties/pdf/projected_coordinate_systems.pd...

For foot use WKID 9002. For Statute_Mile, use wkid 9093

0 Kudos