WPF Bing 地图将图钉放置在错误的位置
WPF Bing Maps places push pin at incorrect position
我已按照 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);
}
知道为什么我的图钉没有放在正确的位置吗?
谢谢
乔
您必须获取相对于地图控件的视口点,而不是声明 MapWithPushpins_MouseDoubleClick
方法的 Window 或 UserControl。
将传递给 GetPosition
的参数从 this
更改为 Map
:
var mousePosition = e.GetPosition(Map);
或sender
参数,它也引用地图控件:
var mousePosition = e.GetPosition((UIElement)sender);
我已按照 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);
}
知道为什么我的图钉没有放在正确的位置吗?
谢谢 乔
您必须获取相对于地图控件的视口点,而不是声明 MapWithPushpins_MouseDoubleClick
方法的 Window 或 UserControl。
将传递给 GetPosition
的参数从 this
更改为 Map
:
var mousePosition = e.GetPosition(Map);
或sender
参数,它也引用地图控件:
var mousePosition = e.GetPosition((UIElement)sender);