WPF Bing Maps将推针放在错误的位置

时间:2015-02-24 12:50:55

标签: c# wpf maps bing-maps bing

我已按照https://msdn.microsoft.com/en-GB/library/hh709044.aspx上的教程了解如何在地图上放置图钉。

除了引脚始终位于我的鼠标指针下方约3厘米处之外,这一切都有效。

我不确定我做错了什么。

继承我的xaml代码

            <Grid HorizontalAlignment="Stretch" Margin="6,6,6,6" Name="gpsGrid" VerticalAlignment="Stretch">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <Label Name="latitudeLabel" Content="Latitude: " Grid.Row="0" Grid.Column="0"/>
            <Label Name="longitudeLabel" Content="Longitude: " Grid.Row="1" Grid.Column="0"/>
            <Label Name="altitudeLabel" Content="Altitude: " Grid.Row="2" Grid.Column="0"/>
            <Label Name="courseLabel" Content="Course: " Grid.Row="3" Grid.Column="0"/>
            <Label Name="speedLabel" Content="Speed: " Grid.Row="4" Grid.Column="0"/>
            <Label Name="gpsConnectedLabel" Content="Connected: " Grid.Row="5" Grid.Column="0"/>
            <m:Map CredentialsProvider="XXXXXXXXXXXXXXXXXXXXXX" Center="53.7997,-1.5492" ZoomLevel="16" Mode="Aerial" x:Name="Map" MouseDoubleClick="MapWithPushpins_MouseDoubleClick"  Grid.Row="6" Grid.Column="0"/>
        </Grid>

和我的点击处理程序

            private void MapWithPushpins_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
              // Disables the default mouse double-click action.
              e.Handled = true;

              //Get the mouse click coordinates
              Point mousePosition = e.GetPosition(this);

              //Convert the mouse coordinates to a locatoin on the map
              Location pinLocation = Map.ViewportPointToLocation(mousePosition);

              // The pushpin to add to the map.
              Pushpin pin = new Pushpin();
              pin.Location = pinLocation;

              // Adds the pushpin to the map.
              Map.Children.Add(pin);
        }

为什么我的针脚没有放在正确位置的任何想法?

由于 乔

1 个答案:

答案 0 :(得分:4)

您必须获取相对于Map控件的视口点,而不是声明MapWithPushpins_MouseDoubleClick方法的Window或UserControl。

将传递给GetPosition的参数从this更改为Map

var mousePosition = e.GetPosition(Map);

sender参数,它也引用了Map控件:

var mousePosition = e.GetPosition((UIElement)sender);