Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center and Zoom on Bing Maps WPF

Tags:

c#

wpf

bing-maps

I created a Bing Maps WPF dialog box and would like set the center and zoom level programmatically. I have tried using SetValue(), but I haven't found the right property to pass to it.

Here is the XAML for my Bing Maps dialog:

<Window 
        x:Class="RAPMkI.BingMapsDialog"
        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="BingMapsDialog" Height="378" Width="467">
    <Grid>
        <m:Map CredentialsProvider="Haha, nice try."/>
        <Button Content="Locate" Margin="0,0,0,0" Name="button1" HorizontalAlignment="Right" Width="Auto" Height="Auto" VerticalAlignment="Top" />
    </Grid>
</Window>

The code-behind looks like this:

namespace RAPMkI
{
    /// <summary>
    /// Interaction logic for BingMapsDialog.xaml
    /// </summary>
    public partial class BingMapsDialog : Window
    {
        public BingMapsDialog(Location center, int zoom)
        {
            InitializeComponent();
        }
    }
}

Is there a way to set the center and zoom level of my dialog box upon initialization, using the Location and zoom that I have passed it?

like image 344
Jason L Avatar asked Dec 13 '22 00:12

Jason L


1 Answers

I realize this is an older question, but the answer accepted is not correct anymore, if it ever was, so I'm hoping this will help someone else.

Center is property not a method so trying to set it won't work. I banged my head against the wall for a while on that too, and kept ending up off the West coast of Africa (Lat: 0, Long: 0).

What you're looking for is SetView(Location location, Double Zoom)

Here's the reference for that:
https://msdn.microsoft.com/en-us/library/hh709343.aspx

To rewrite the example above:

public BingMapsDialog(Location center, double zoom)
{
    InitializeComponent();
    theMap.SetView(center, zoom);
}

Should be all that's needed.

like image 62
Graham Bass Avatar answered Dec 27 '22 16:12

Graham Bass