WPF Bing Maps - Zoom to Polyline

Viewed 207

I created a WPF Bing map and added polyline I would like set the center and zoom level, which fit polyline. Like map.fitBounds(bounds).

MapPolyline polyline = new MapPolyline();
polyline.Stroke = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Blue);
polyline.Locations = new LocationCollection() { 
    new Location(47.6424, ,-122.3219), 
    new Location(47.8424,-122.1747), 
    new Location(47.67856,-122.130994)};

myMap.Children.Add(polyline);
1 Answers

You can get an IEnumerable<Location> from LocationCollection of your polyline and then use an overload of SetView to zoom to the locations. This overload allows you to set a margin as well.

myMap.SetView(polyline.Locations.Cast<Location>(), 
    new System.Windows.Thickness(0), 0);

Or you can create a LocationRect from LocationCollection of your polyline and then use another overload of SetView to zoom to the rectangle.

myMap.SetView(new LocationRect(polyline.Locations));

Exampel 1 - IEnumerable<Location>

MapPolyline polyline = new MapPolyline();
polyline.Stroke = new SolidColorBrush(Colors.Blue);
polyline.Locations = new LocationCollection() {
    new Location(47.6424, -122.3219),
    new Location(47.8424,-122.1747),
    new Location(47.67856,-122.130994)};
myMap.Children.Add(polyline);
myMap.SetView(polyline.Locations.Cast<Location>(), 
    new System.Windows.Thickness(0), 0);

Example 2 - LocationRect

MapPolyline polyline = new MapPolyline();
polyline.Stroke = new SolidColorBrush(Colors.Blue);
polyline.Locations = new LocationCollection() {
    new Location(47.6424, -122.3219),
    new Location(47.8424,-122.1747),
    new Location(47.67856,-122.130994)};
myMap.Children.Add(polyline);
myMap.SetView(new LocationRect(polyline.Locations));

enter image description here

Related