Center and Zoom on Bing Maps WPF

不羁岁月 提交于 2019-12-10 22:09:53

问题


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?


回答1:


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.




回答2:


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.




回答3:


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"




回答4:


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


来源:https://stackoverflow.com/questions/11549166/center-and-zoom-on-bing-maps-wpf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!