Adding a dynamic scale using SmartText in MapInfo Pro

Products affected: MapInfo Pro 64 bit, MapBasic
 
Insert a text box in the Layout and add the following code:

Adding a map scale bar with SmartText
To add a scale bar use the code below

$mb{MapperInfo(LayoutItemInfo(CurContext(1), "Map-1", 5), 33)}


Adding the scale in text with SmartText
With the code below, the scale text will automatically be updated with the scale selected in the Change View dialog box.

Scale: 1 $mb{SessionInfo(4)} = $mb{MapperInfo(LayoutItemInfo(CurContext(1), "Map-1", 5), 2)} $mb{MapperInfo(LayoutItemInfo(CurContext(1), "Map-1", 5), 12)}

The layout frame in the above code is called "Map-1". To call the frame Map-1 right click the layout frame and select frame properties

Next activate the layout frame by double clicking on it and then right click and select 'Change View', then select the desired Cartographic scale. The scale gets automatically updated within the Scale text box with the scale selected in the Change View dialog box.

The grounds units can be changed by changing the Distance Units in Map Options. Double click on the template page to activate it, go to Map Options / Settings. Change the Distance Units, select the option Cartesian, click on OK. Then zoom in and out to and the scale is recalculated within the smart text. 


Understanding the scale bar code
  • $mb(<something>) is Smart Text. It will execute the MapBasic command inside of the parenthesis.
  • The "1" will get you the current layout window ID.
  • LayoutItemInfo() will get you the map frame window ID for the frame that is named "Map-1". The frame can be given any name and in this example it is called "Map-1", right click the frame and go to properties to name the layout frame. 
  • Once you have a map window ID, you can use it in MapperInfo().
Understanding the scale in text code
  • $mb(<something>) is Smart Text. It will execute the MapBasic command inside of the parenthesis.
  • SessionInfo(4) returns the current Paper units, ensure that this is correctly set by going to MapInfo Pro Options / Settings
  • $mb{MapperInfo(LayoutItemInfo(CurContext(1), "Map-1", 5), 2)}  The first parameter for MapperInfo() is a mapper window ID, this returns the current scale for the map window. This uses a bunch of nested MapBasic calls to find the Map window ID in the current layout window.
  1. (LayoutItemInfo(CurContext(1), "Map-1", 5)
  2. CurContext() is special function used for SmartText. Parameter 1 will return the layout window ID:                   
  3. LayoutItemInfo() will return the window ID for the map frame with the name “Map-1” in that layout from parameter 1. The frame can be given any name, right click the frame and go to properties for this. 
  • MapperInfo(…, 2) returns the current scale for the map window
UPDATED:  March 5, 2020