使用边界矩形的WPF Microsoft Bing Control SetView

时间:2014-02-10 15:45:37

标签: c# wpf bing-maps

鉴于以下代码我在WPF中创建了一个Bing地图,并添加了两个点(Ind和ATL)一切正常,除了我试图让地图自动缩放并在我的两点之间居中。我收到以下异常。任何人都对我做错了什么有任何想法?谢谢!

{"The actual size of the control must be positive and finite in order to set the view using a bounding rectangle."}

<Window x:Class="MapSetCenter.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:m="clr-namespace:Microsoft.Maps.MapControl.WPF;assembly=Microsoft.Maps.MapControl.WPF" 
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <m:Map x:Name="myMap" CredentialsProvider="YourKeyHere"></m:Map>
    </Grid>
</Window>

public partial class MainWindow : Window
{
    public class Trip
    {
        public string Name { get; set; }

        public double OriginLatitude { get; set; }
        public double OriginLongitute { get; set; }

        public double DestinationLatitude { get; set; }
        public double DestinationLongitude { get; set; }
    }

    public MainWindow()
    {
        InitializeComponent();

        var trip = new Trip();
        trip.Name = "INDY-ATL";
        trip.OriginLatitude = 39.768403;
        trip.OriginLongitute = -86.158068;
        trip.DestinationLatitude = 33.748995;
        trip.DestinationLongitude = -84.387982;

        var origin = new Pushpin();
        origin.Location = new Location(trip.OriginLatitude, trip.OriginLongitute);

        var destination = new Pushpin();
        destination.Location = new Location(trip.DestinationLatitude, trip.DestinationLongitude);

        // Adds the pushpin to the map.
        myMap.Children.Add(origin);
        myMap.Children.Add(destination);

        var locations = new List<Location>();
        locations.Add(origin.Location);
        locations.Add(destination.Location);

        // problem code below

        //LocationRect boundingBox = new LocationRect(locations);
        //myMap.SetView(boundingBox);
    }
}

2 个答案:

答案 0 :(得分:3)

要尝试的几件事情:

在加载地图之前,请勿设置地图视图。像这样添加一个Loaded事件:

myMap.Loaded += (s,e)=>{myMap.SetView(boundingBox);};

如果仍有问题,请尝试在SetView行上设置断点,并查看boundingBox的值。验证它是否具有有效值。

答案 1 :(得分:2)

尝试以下方法对我有用:

MyMap.Loaded += (s, e) =>
{
    var origin = new Pushpin();
    origin.Location = new Location(39.768403, -86.158068);

    var destination = new Pushpin();
    destination.Location = new Location(33.748995, -84.387982);

    // Adds the pushpin to the map.
    MyMap.Children.Add(origin);
    MyMap.Children.Add(destination);

    var locations = new List<Location>();
    locations.Add(origin.Location);
    locations.Add(destination.Location);

    // problem code below

    LocationRect boundingBox = new LocationRect(locations);
    MyMap.SetView(boundingBox);
};

唯一的问题是只出现一个图钉。其原因是边界框基于坐标,因此第二图钉的尖端实际上在视野中。为了解决这个问题,我们需要缓冲边界框。实现此目的的一种简单方法实际上是使用不同版本的SetView方法,如下所示:

MyMap.SetView(locations, new Thickness(30), MyMap.Heading);