在Bing Maps WPF上居中并放大

时间:2012-07-18 19:49:22

标签: c# wpf bing-maps

我创建了一个Bing Maps WPF对话框,并希望以编程方式设置中心和缩放级别。我尝试过使用SetValue(),但我找不到合适的属性来传递给它。

以下是Bing Maps对话框的XAML:

<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>

代码隐藏看起来像这样:

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

有没有办法在初始化时设置对话框的中心和缩放级别,使用Location和我已经通过的缩放?

4 个答案:

答案 0 :(得分:7)

我意识到这是一个较老的问题,但接受的答案已不再正确,如果有的话,所以我希望这会帮助其他人。

Center属性不是方法,因此尝试设置它将无效。我也把头撞在墙上一段时间,并且一直在非洲西海岸结束(Lat:0,Long:0)。

您正在寻找的是SetView(Location location, Double Zoom)

以下是对此的参考:
https://msdn.microsoft.com/en-us/library/hh709343.aspx

重写上面的例子:

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

应该是所有需要的。

答案 1 :(得分:4)

首先需要为地图指定一个名称,以便以编程方式访问它。 e.g:

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

然后将CenterZoomLevel属性设置为您想要更改的位置。例如:

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

如果这不起作用,您可能需要在Center事件处理程序中设置ZoomLevelLoaded

答案 2 :(得分:1)

Map有两个可绑定属性:ZoomLevel和Center。 您可以做的是将视图绑定到视图模型,该视图模型具有两个表示ZoomLevel和Center的属性。

ZoomLevel是double,所以可以做的一件好事就是添加一个滑块,它的值也绑定到viewmodel中表示zoomlevel的值。这样您就可以使用滑块更改缩放。

*注意你需要制作绑定模式&#34; TwoWays&#34;

答案 3 :(得分:0)

您可以在XAML文件中定义初始位置,如:

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

然后以编程方式设置中心和缩放级别,如:

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

或单独:

mMap.Center = mylocation
mMap.ZoomLevel = myzoomlevel
相关问题