Add a polygon to the Silverlight Bing Map

In this article we will be seeing how to add a polygon on top of the Silverlight Bing Map.

Refer this Article  for creating Silverlight Bing Map Control.

MapPolygon:

The MapPolygon class accepts a list of points that define its shape and location on the map.

MapPolygon class is used to represent a polygon on the map.

Namespace: Microsoft.Maps.MapControl

Assembly: Microsoft.Maps.MapControl (in microsoft.maps.mapcontrol.dll)

Create Silverlight application:

1. Open Visual Studio 2010.

2. Go to File => New => Project.

3. Select Silverlight Application from the installed templates Silverlight.

4. Enter the Name and click on Ok.

5. Click OK.

6. Go to C:\Program Files (x86)\Bing Maps Silverlight Control\V1\Libraries\ (Check the path where you have installed Bing Maps Silverlight Control).

7. Add the following assemblies to the project.

o Microsoft.Maps.MapControl.Common.dll o Microsoft.Maps.MapControl.dll

8. Replace MainPage.xaml with the following code

  1. <UserControl x:Class="SilverlightMapControl.MainPage"

  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  

  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  

  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  

  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

  6. xmlns:map="clr-namespace:Microsoft.Maps.MapControl;assembly=Microsoft.Maps.MapControl" mc:Ignorable="d"

  7. d:DesignHeight="300" d:DesignWidth="400">

  8. <Grid x:Name="LayoutRoot" Background="White">

  9. <Grid.RowDefinitions>

  10. <RowDefinition Height="35" />

  11. <RowDefinition Height="*" />

  12. </Grid.RowDefinitions>

  13. <Button x:Name="btnCreatePolygon" Width="100" Height="25" Content="Create Polygon" HorizontalAlignment="Left" Tag="false" Click="btnCreatePolygon_Click">

  14. </Button>

  15. <map:Map x:Name="MapWithPolygon" CredentialsProvider="your key" Mode="Road"Grid.Row="1" Grid.ColumnSpan="2">

  16. </map:Map>

  17. </Grid>

  18. </UserControl>

9. Add the following in MainPage.xaml.cs.

  1. private void btnCreatePolygon_Click(object sender, RoutedEventArgs e)       

  2. {

  3. MapPolygon polygon = new MapPolygon();           

  4. polygon.Fill = newSystem.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Gray);           

  5. polygon.Stroke = newSystem.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Black);           

  6. polygon.StrokeThickness = 1;           

  7. polygon.Opacity = 0.7;           

  8. polygon.Locations = new LocationCollection() {

  9. new Location(15,-15),

  10. new Location(15,15),

  11. new Location(-15,15),

  12. new Location(-15,-15)};           

  13. MapWithPolygon.Children.Add(polygon);       

  14. }

10. Build the solution.

11. Hit F5.

12. The output looks like the following


Location:

Contains the altitude and coordinate values of a location on the map.

Fill:

It is used to get or set the fill of the shape.

Opacity:

It is used to get or set the opacity of the shape.

Stroke:

It is used to get or set the stroke color of the shape.

erver'>

Recent Posts

See All