Version

Add Surface Element

A SurfaceElement object represents polygons and shapes in xamMap™. You can add your own SurfaceElement objects to display custom shapes. You must define at least 3 points, specified in either Geodetic or Cartesian coordinates, to create a custom shape.

The following code shows you how to create a boat that is docked on the east coast of the United States, using the Imported event of the map layer.

In Visual Basic:

Private Sub worldLayer_Imported(ByVal sender As System.Object, ByVal e As Infragistics.Controls.Maps.MapLayerImportEventArgs)
   If e.Action = MapLayerImportAction.[End] Then
      ' Polyline collection to hold points for shape
      Dim lines As New MapPolylineCollection()
      Dim points As New List(Of Point)()
      ' Points to make the shape
      points.Add(New Point(-74.535 - 2, 40.246 - 1))
      points.Add(New Point(-74.535 + 2, 40.246 - 1))
      points.Add(New Point(-74.535 + 4, 40.246 + 1))
      points.Add(New Point(-74.535 - 4, 40.246 + 1))
      ' Convert Geodetic to Cartesian coordinates
      lines.Add(map1.MapProjection.ProjectToMap(points))
      ' Create surface element and position shape using polylines
      Dim surfaceElement As New SurfaceElement()
      surfaceElement.Polylines = lines
      surfaceElement.Fill = New SolidColorBrush(Colors.Brown)
      map1.Layers(0).Elements.Add(surfaceElement)
      Dim worldRect As Rect = surfaceElement.WorldRect
      worldRect = lines.GetWorldRect()
      surfaceElement.WorldRect = worldRect
   End If
End Sub

In C#:

private void worldLayer_Imported(object sender, Infragistics.Controls.Maps.MapLayerImportEventArgs e)
{
   if (e.Action == MapLayerImportAction.End)
   {
      // Polyline collection to hold points for shape
      MapPolylineCollection lines = new MapPolylineCollection();
      List<Point> points = new List<Point>();
      // Points to make the shape
      points.Add(new Point(-74.535 - 2, 40.246 - 1));
      points.Add(new Point(-74.535 + 2, 40.246 - 1));
      points.Add(new Point(-74.535 + 4, 40.246 + 1));
      points.Add(new Point(-74.535 - 4, 40.246 + 1));
      // Convert Geodetic to Cartesian coordinates
      lines.Add(map1.MapProjection.ProjectToMap(points));
      // Create surface element and position shape using polylines
      SurfaceElement surfaceElement = new SurfaceElement() { Polylines = lines, Fill = new SolidColorBrush(Colors.Brown) };
      map1.Layers[0].Elements.Add(surfaceElement);
      Rect worldRect = surfaceElement.WorldRect;
      worldRect = lines.GetWorldRect();
      surfaceElement.WorldRect = worldRect;
   }
}
xamMap Add Surface Element 01.png