Welcome Guest, you are in: Login

FDOT Wiki

RSS RSS

GisFramework



Search the wiki
»

DrawingGeometry

RSS
Modified on Friday, 15 July 2011 02:55 PM by 156.75.200.132 Categorized as Uncategorized
There are essentially three cases for rendering a drawing with the framework:

  • Drawing an object on the map programatically.
  • Allowing a user to draw an object.
  • Allowing the user to draw an object and get feedback (e.g. updated measurements) while the object is being drawn.

The primary classes involved are the GraphicsManager and the GeometrySelectionService. The GraphicsManager manages graphic objects after they've been rendered, while the GeometrySelectionService manages graphic objects while they're being drawn. So, to draw an object programmatically, the GraphicsManager is sufficient, while user interaction requires both the GeometrySelectionService, to manage the object while it's being drawn, and the GraphicsManager, to place the object when the drawing is complete.

Example - drawing an object.


// The graphic can be drawn with a DataContracts.Geometry object
// using AddGraphic(DataContracts.Geometry, Symbol, MapTip, 
//                  IEnumerable<KeyValuePair<string,object>> Attributes)
GraphicsManager graphicsManager = GraphicManagerFactory.CreateGraphicsManager();
graphicsManager.AddGraphic(geometry, null, null, null);

// or with a Feature using AddGraphic(Feature, Symbol, MapTip)
graphicsManager.AddGraphic(feature, null, null);

It should be pointed out that, in the above example, Feature has properties for its Geometry and Attributes, so those need not be specified in that overload.

For drawing interaction, the GeometrySelectionService and GraphicsManager will need to be used, so the following will be used for the interactive drawing examples below:

bool isDrawing = false;

GraphicsManager graphicsManager = GraphicManagerFactory.CreateGraphicsManager();

GeometrySelectionService geometryService = new GeometrySelectionService(
                                    new GeometrySelectionServiceConfiguration 
                                        { 
                                            ShowLabels = true, 
                                            Color = Colors.Green 
                                        });

The GeometrySelectionService will need a callback for the drawing completed event. The drawing completed event will use the GraphicsManager to place the final drawing, as follows:

private void DrawCompleteCallback(DataContracts.Geometry geometry)
{
    isDrawing = false;
    graphicsManager.AddGraphic(geometry, SymbolManager.GetFillSymbol("#FF0000"), null, null);
}

Example - drawing an object interactively.



if (isDrawing)
{ geometryService.CancelSelection(); }
isDrawing = true;

// This example uses the DrawCompleteCallback
geometryService.SelectGeometry(GeometryType.Polyline, DrawCompleteCallback);

// The same thing using a lambda expression
geometryService.SelectGeometry(GeometryType.Polyline, g =>
{
    // The drawing is done, so set the flag and add the graphic to the map.
    // Note that the symbol, map tip, and attributes can also be set on the drawing object here.
    isDrawing = false;
    graphicsManager.AddGraphic(g, null, null, null);
});

If something needs to occur while the object is being drawn, the GeometrySelectionService will also need a callback for the drawing changed event.

private void GeometryChangeDelegate(object sender, GeometryChangedEventArgs e)
{
    // We can get a reference to the geometry being drawn from the GeometryChangedEventArgs
    DataContracts.Geometry envelope = e.CurrentGeometry;

    // And do something with the object as it's being drawn.  The following uses the MeasureManager to
    // calculcate the perimiter and area of the envelope.
    DataContracts.Geometry polygon = GeometryTranslator.ConvertEnvelopeToPolygon(envelope);
    Area = Math.Round(MeasureManager.GetMeasureManager(_linearUnit, _areaUnit).GetPolygonArea(polygon), 2).ToString();
    Perimeter = Math.Round(MeasureManager.GetMeasureManager(_linearUnit, _areaUnit).GetPerimeter(polygon), 2).ToString();
}

Example - drawing an object interactively with a drawing changed callback.


geometryService.SelectGeometry(GeometryType.Box, DrawCompleteCallback, 
                               "Select geometry for measurement.", GeometryChangeDelegate, 
                               Unit.Mile, SquareUnit.SquareMile);

Related Topics

Working With Colors, Fills, and Symbols

See Also

Any Questions or Comments? Email
Some of the icons created by FamFamFam.