WPF Bing 地图 - 缩放到折线

WPF Bing Maps - Zoom to Polyline

我创建了一个 WPF Bing 地图并添加了折线我想设置适合折线的中心和缩放级别。像 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);

您可以从 LocationCollection of your polyline and then use an overload of SetView 获取 IEnumerable<Location> 以缩放到这些位置。此重载还允许您设置边距。

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

或者您可以创建一个 LocationRect from LocationCollection of your polyline and then use another overload of SetView 来缩放到矩形。

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

示例 1 - IEnumerable

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);

示例 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));