Adding Polylines and Polygons in Bing Maps Apps

Currently the Bing Maps App SDK .chm documentation covers how to add Pushpins on the map, but not polylines and polygons.  When creating your entities, rather than passing in a PointPrimitive instance to the Entity.Primitive property, you can pass in a PolylinePrimitive or a PolygonPrimitive instance instead.  The constructor for PoylinePrimitive and PolygonPrimitive takes in a collection of locations, and a Brush instance to indicate the line color/polygon fill.  The following example adds a Polyline to a map (assumes your Map/Layers/Entities are registered correctly as per the SDK):

                Location point1 = new Location(0, 0);
                Location point2 = new Location(0, 50);
                Location point3 = new Location(-25, 25);

                LocationCollection locationCollection = new LocationCollection();
                locationCollection.Add(point1);
                locationCollection.Add(point2);
                locationCollection.Add(point3);

                SolidColorBrush mySolidColorBrush = new SolidColorBrush();
                mySolidColorBrush.Color = Color.FromArgb(255, 0, 0, 255);

                PolylinePrimitive polyline = new PolylinePrimitive(locationCollection, mySolidColorBrush);

                // Assume Entity instance exists and we will be adding it to our Map Layer
                entity.Primitive = polyline;

0741.polyline Developer

Figure 1.0 – Adding a polyline to the Bing Maps App, viewed through the Map App Test Tool

The following sample adds a polygon in the same location.  Note that the only real difference is that we are passing in a PolygonPrimitive type and it’s constructor takes a third Brush parameter to represent the fill:

                Location point1 = new Location(0, 0);
               
Location point2 = new Location(0, 50);
                Location
point3 = new Location(-25, 25);

               
LocationCollection locationCollection = new LocationCollection();
               
locationCollection.Add(point1);
               
locationCollection.Add(point2);
               
locationCollection.Add(point3);

                SolidColorBrush
mySolidColorBrush = new SolidColorBrush();
               
mySolidColorBrush.Color = Color.FromArgb(255, 0, 0, 255);

                PolygonPrimitive
polygon = new PolygonPrimitive(locationCollection, mySolidColorBrush,mySolidColorBrush);

               
// Assume Entity instance exists and we will be adding it to our Map
Layer
                entity.Primitive = polygon;

8130.polygon Developer

Figure 2.0 – Adding a polygon to the Bing Maps App, viewed through the
Map App Test Tool