Back to Devexpress

Measurements Class

windowsforms-devexpress-dot-xtramap-bfc844d4.md

latest20.1 KB
Original Source

Measurements Class

Contains API to manage rulers that help users measure distances and areas on a map.

Namespace : DevExpress.XtraMap

Assembly : DevExpress.XtraMap.v25.2.dll

NuGet Package : DevExpress.Win.Map

Declaration

csharp
public class Measurements :
    IMapEditor,
    IRulerActionListener,
    IMapRulerStyleProvider,
    IMapStyleOwner,
    IMapOverlayActionListener
vb
Public Class Measurements
    Implements IMapEditor,
               IRulerActionListener,
               IMapRulerStyleProvider,
               IMapStyleOwner,
               IMapOverlayActionListener

The following members return Measurements objects:

Remarks

The Measurements class allows you to manage distance and area rulers on a map.

Distance RulerArea Ruler

Measurements Toolbar

The Measurements toolbar contains buttons that allow users to add rulers to a map. To display this toolbar, set the ShowToolbar property to true in the Properties window at design time.

Use the code below to show the toolbar at runtime.

csharp
mapControl1.Measurements.ShowToolbar = true;
vb
mapControl1.Measurements.ShowToolbar = True

Toolbar buttons include:

|

Button

|

Name

|

Visibility

| | --- | --- | --- | |

|

Add Distance Ruler

|

ToolbarOptions.ShowDistanceButton

| |

|

Add Area Ruler

|

ToolbarOptions.ShowAreaButton

|

Distance Ruler

The distance ruler calculates the distance between two or more points on a map. The total distance is shown in a map callout. Note that measurements ignore changes in elevation.

Click an intermediate point on the ruler to see the distance to this point.

To specify the measurement unit for the newly created distance ruler, set the DistanceUnits property to one of the following values:

To apply the distance unit to the ruler, specify the unit before you create the ruler. The following code creates a ruler that measures distance in miles:

csharp
mapControl1.Measurements.DistanceUnits = MeasureUnit.Mile;
MapRuler ruler= mapControl1.Measurements.CreateRuler(RulerType.Distance, new List<CoordPoint>() {
                                                                                  new GeoPoint(0, 0),
                                                                                  new GeoPoint(10, 0),
                                                                                  new GeoPoint(10, 10)});
vb
mapControl1.Measurements.DistanceUnits = MeasureUnit.Mile
Dim ruler As MapRuler = mapControl1.Measurements.CreateRuler(RulerType.Distance, 
                                                              New List(Of CoordPoint)() From {
                                                                            New GeoPoint(0, 0),
                                                                            New GeoPoint(10, 0),
                                                                            New GeoPoint(10, 10)})

Area Ruler

The area ruler calculates the area inside a polygon. The result is shown in a map callout.

To define the area measurement unit, set the AreaUnits property to one of the following values:

To apply the area unit to the ruler, specify the unit before you create the ruler. The following code creates a ruler that measures the area in square miles:

csharp
mapControl1.Measurements.AreaUnits = AreaMeasurementUnit.SquareMile;
MapRuler ruler= mapControl1.Measurements.CreateRuler(RulerType.Area, new List<CoordPoint>() {
                                                                                  new GeoPoint(0, 0),
                                                                                  new GeoPoint(10, 0),
                                                                                  new GeoPoint(10, 10) });
vb
mapControl1.Measurements.AreaUnits = AreaMeasurementUnit.SquareMile
Dim ruler As MapRuler = mapControl1.Measurements.CreateRuler(RulerType.Area, 
                                                              New List(Of CoordPoint)() From {
                                                                            New GeoPoint(0, 0),
                                                                            New GeoPoint(10, 0),
                                                                            New GeoPoint(10, 10)})

Ruler Management

Create Rulers

To create a ruler, a user can click the Add Distance Ruler or Add Area Ruler toolbar button, click the map to set ruler points, and then double-click the map to complete the ruler.

The following image shows how to create an area ruler:

You can also call the SetCreateMode(RulerType) method to activate the Measurements object’s Create mode. In this mode, users can click the map to create a ruler. They do not need to click toolbar buttons.

To create a ruler programmatically, call the CreateRuler(RulerType, IList<CoordPoint>) method. The following code creates a distance ruler:

csharp
MapRuler ruler = mapControl1.Measurements.CreateRuler(RulerType.Distance, 
                                                        new List<CoordPoint>(){
                                                           new GeoPoint(48.864716, 2.349014), // Paris
                                                           new GeoPoint(45.46427, 9.18951), // Milan
                                                           new GeoPoint(48.20849, 16.37208) }); // Vienna
vb
Dim ruler As MapRuler = mapControl1.Measurements.CreateRuler(RulerType.Distance, 
                                                             New List(Of CoordPoint)() From {
                                                                New GeoPoint(48.864716, 2.349014), ' Paris
                                                                New GeoPoint(45.46427, 9.18951), ' Milan 
                                                                New GeoPoint(48.20849, 16.37208) }) ' Vienna

Configure Rulers

After a ruler is created, the Measurements object activates Edit mode. The SetEditMode() method also enables Edit mode.

The following actions are available in Edit mode:

|

Action

|

Description

| | --- | --- | |

Add a ruler point.

|

Click the position on the ruler where you wish to add a point.

| |

Change the ruler polyline.

|

Click and drag the point to a new location.

| |

Delete a ruler point.

|

Double-click the ruler point.

|

The image below shows how to change a distance ruler:

Use the following methods to configure a ruler programmatically:

|

Name

|

Description

| | --- | --- | |

InsertPoint(MapRuler, CoordPoint, Int32)

|

Adds a new point to the ruler.

| |

RemovePoint(MapRuler, Int32)

|

Removes the point with the specified index.

| |

UpdatePoint(MapRuler, CoordPoint, Int32)

|

Changes the coordinates of the point with the specified index.

|

The example below creates a ruler to measure the distance between Paris, Milan and Vienna. After calculating this distance, the code updates the ruler as follows:

  • a point is added at Berlin’s coordinates;

  • the first point is moved from Paris to Dusseldorf;

  • the point at Milan’s coordinates is removed from the ruler.

  • C#

  • VB.NET

csharp
MapRuler ruler = mapControl1.Measurements.CreateRuler(RulerType.Distance,
                                                        new List<CoordPoint>() {
                                                            new GeoPoint(48.864716, 2.349014), // Paris
                                                            new GeoPoint(45.46427, 9.18951), // Milan
                                                            new GeoPoint(48.20849, 16.37208) // Vienna
                                                        });
//The code below updates the ruler.                                                        
mapControl1.Measurements.InsertPoint(ruler, new GeoPoint(52.520008, 13.404954), 2); // Berlin
mapControl1.Measurements.UpdatePoint(ruler, new GeoPoint(51.2217200, 6.7761600), 0); // Dusseldorf
mapControl1.Measurements.RemovePoint(ruler, 1); // Milan
vb
Dim ruler As MapRuler = mapControl1.Measurements.CreateRuler(RulerType.Distance, 
                                                             New List(Of CoordPoint)() From {
                                                                New GeoPoint(48.864716, 2.349014),' Paris
                                                                New GeoPoint(45.46427, 9.18951), ' Milan
                                                                New GeoPoint(48.20849, 16.37208) ' Vienna })

'The code below updates the ruler.                                                        
mapControl1.Measurements.InsertPoint(ruler, New GeoPoint(52.520008, 13.404954), 2) ' Berlin
mapControl1.Measurements.UpdatePoint(ruler, New GeoPoint(51.2217200, 6.7761600), 0) ' Dusseldorf
mapControl1.Measurements.RemovePoint(ruler, 1) ' Milan
Before UpdateAfter Update

Remove Rulers

To remove a ruler, a user can click the cross button on the map callout that displays the ruler’s measurement value:

The RemoveRuler(MapRuler) method removes the ruler passed as a parameter.

csharp
mapControl1.Measurements.RemoveRuler(ruler);
vb
mapControl1.Measurements.RemoveRuler(ruler)

To delete all rulers from the map, call the RemoveRulers() method.

csharp
mapControl1.Measurements.RemoveRulers();
vb
mapControl1.Measurements.RemoveRulers()

Control Ruler Creation

You can handle the BeforeMeasurement and AfterMeasurement events to implement custom logic during ruler creation.

The example below cancels ruler creation if a ruler starts inside an ellipse. The MapControl.CalcHitInfo method returns information on the map elements located at the ruler’s first point. The e.StartPoint property returns this point.

csharp
private void Measurements_BeforeMeasurement(object sender, BeforeMeasurementEventArgs e) {
 MapHitInfo info = this.mapControl1.CalcHitInfo(e.StartPoint);
 if (info.InMapEllipse) {
     e.Cancel = true;
 }    
}
vb
Private Sub Measurements_BeforeMeasurement(ByVal sender As Object, ByVal e As BeforeMeasurementEventArgs)
 Dim info As MapHitInfo = Me.mapControl1.CalcHitInfo(e.StartPoint)
 If info.InMapEllipse Then
     e.Cancel = True
 End If
End Sub

The following example cancels area ruler creation. First, use the e.MapRuler property in the AfterMeasurement event handler to get the newly created ruler. To determine whether this ruler is an area ruler, use the MapRuler.RulerType property. If it is an area ruler, set the e.Cancel property to true to prevent the ruler from being displayed.

csharp
private void Measurements_AfterMeasurement(object sender, AfterMeasurementEventArgs e) {
  MapRuler ruler1 = e.MapRuler;
  if (ruler1.RulerType == RulerType.Area) {
      e.Cancel = true;
  }
}
vb
Private Sub Measurements_AfterMeasurement(ByVal sender As Object, ByVal e As AfterMeasurementEventArgs)
  Dim ruler1 As MapRuler = e.MapRuler

  If ruler1.RulerType Is RulerType.Area Then
      e.Cancel = True
  End If
End Sub

Tip

You can also use the GeoUtils class to calculate geometric values on the map. This class contains cartography measurement API for maps with a geographic coordinate system.

Customize Ruler Appearance

Use the Style property to define the style for all rulers on the map.

csharp
mapControl1.Measurements.Style.Fill = Color.OrangeRed;
mapControl1.Measurements.Style.AreaTransparency = 100;
mapControl1.Measurements.Style.Stroke = Color.DarkRed;
mapControl1.Measurements.Style.StrokeWidth = 2;
vb
mapControl1.Measurements.Style.Fill = Color.OrangeRed
mapControl1.Measurements.Style.AreaTransparency = 100
mapControl1.Measurements.Style.Stroke = Color.DarkRed
mapControl1.Measurements.Style.StrokeWidth = 2

The fill color is also used to draw the outline border of area rulers and distance ruler borders.

To change style settings for a newly created ruler, handle the BeforeMeasurement event. Use the e.Style property to define a new ruler style as follows:

csharp
private void Measurements_BeforeMeasurement(object sender, BeforeMeasurementEventArgs e) {
  e.Style.Stroke = Color.Gray;
  e.Style.StrokeWidth = 4; 
}
vb
Private Sub Measurements_BeforeMeasurement(ByVal sender As Object, ByVal e As BeforeMeasurementEventArgs)
  e.Style.Stroke = Color.Gray
  e.Style.StrokeWidth = 4
End Sub

You can also call the CreateRuler(RulerType, IList<CoordPoint>, MapRulerStyle) method and pass ruler style options as the style parameter.

The following example changes the ruler’s outline color and width:

csharp
MapRulerStyle style = new MapRulerStyle();
style.Stroke = Color.Red;
style.StrokeWidth = 3;
MapRuler ruler1 = mapControl1.Measurements.CreateRuler(RulerType.Distance, 
                                                      new List<CoordPoint>() {
                                                          new GeoPoint(51.2217200, 6.7761600), // Dusseldorf
                                                          new GeoPoint(52.520008, 13.404954), // Berlin
                                                          new GeoPoint(48.20849, 16.37208) // Vienna
                                                      }, style);
vb
Dim style As MapRulerStyle = New MapRulerStyle()
style.Stroke = Color.Red
style.StrokeWidth = 3
Dim ruler1 As MapRuler = mapControl1.Measurements.CreateRuler(RulerType.Distance, New List(Of CoordPoint)() From {
                                                                                      New GeoPoint(51.2217200, 6.7761600), ' Dusseldorf
                                                                                      New GeoPoint(52.520008, 13.404954), ' Berlin
                                                                                      New GeoPoint(48.20849, 16.37208) ' Vienna
                                                                                  }, style)

Inheritance

Object Measurements

See Also

Measurements Members

GeoUtils

DevExpress.XtraMap Namespace