Question

Given the following code I've created a Bing Map in WPF and added two points (Ind and ATL) Everything works except I'm trying to have the map automatically zoom and center between my two points. I receive the following exception. Anyone have any thoughts on what I'm doing wrong? Thanks!

{"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);
    }
}
Was it helpful?

Solution

A couple of things to try:

Don't set the view of the map until it is loaded. Add a Loaded event like this:

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

If you still have issues try setting a break point on the SetView line and see what the value of the boundingBox is. Verify that it has valid values.

OTHER TIPS

Trying the following worked for me:

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

The only thing with this is that only one pushpins appears. The reason for this is that the bounding box is based on the coordinates and as such the tip of the second pushpin is actually in view. To account for this we need to buffer the bounding box. A simple way to do this is actually to use a different version of the SetView method like this:

MyMap.SetView(locations, new Thickness(30), MyMap.Heading);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top