Skip to main content
 首页 » 编程设计

c#之WPF Bing map 将图钉放置在不正确的位置

2024年02月06日30luoye11

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

除引脚始终位于我的鼠标指针下方约 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); 
        } 

知道为什么我的图钉没有放在正确的位置吗?

谢谢 乔

请您参考如下方法:

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

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

var mousePosition = e.GetPosition(Map); 

或者到 sender 参数,它也引用了 map 控件:

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