Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
506 views
in Technique[技术] by (71.8m points)

c# - WPF Bing Maps - Zoom to Polyline

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...