Jason L
Jason L

Reputation: 1832

Center and Zoom on Bing Maps WPF

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?

Upvotes: 5

Views: 8552

Answers (4)

Carlos Borau
Carlos Borau

Reputation: 1473

You can define the initial position in your XAML file like:

<m:Map x:Name="mMap" 
               CredentialsProvider= "xxxxxxxxxx" 
               Center="40.13618,-0.45822" 
               ZoomLevel="15">
</m:Map>

Then programatically you can set both the center and zoomlevel like:

mMap.SetView(mylocation, myzoomlevel) 'mylocation -> Location, myzoomlevel -> Double

or separately:

mMap.Center = mylocation
mMap.ZoomLevel = myzoomlevel

Upvotes: 0

Ahmad ElMadi
Ahmad ElMadi

Reputation: 2617

Map has two bindable properties: ZoomLevel and Center . What you can do is to bind the view to a view model that has two properties which represent ZoomLevel and Center.

ZoomLevel is double, so a nice thing can be done is by adding a slider that its value is also bound to what represents the zoomlevel in the viewmodel . this way you can change the zoom using the slider.

*Note you need to make the mode of binding "TwoWays"

Upvotes: 1

Graham Bass
Graham Bass

Reputation: 126

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.

Upvotes: 8

Peter Ritchie
Peter Ritchie

Reputation: 35879

You first need to give your map a name so you can access it programmatically. e.g.:

<m:Map Name="theMap" CredentialsProvider="Haha, nice try."/>

Then set the Center and ZoomLevel properties where you'd like them changed. For example:

public BingMapsDialog(Location center, int zoom)
{
    InitializeComponent();
    theMap.Center = center;
    theMap.ZoomLevel = zoom;
}

If that doesn't work, you might need to set Center and ZoomLevel in the Loaded event handler.

Upvotes: 4

Related Questions