简体   繁体   English

如何使用ViewportPointToLocation鼠标单击来检索坐标?

[英]How can I retrieve coordinates on a mouse click with ViewportPointToLocation?

I'm using Bing map WPF control SDK to try retrieving coordinates and printing them 我正在使用Bing地图WPF控件SDK尝试检索坐标并打印它们

I've managed to retrieve the coordinates of the center of the current LocationRect using an EventHandler associated with pressing the arrows I've tried employing the same concept with mouse clicking event handlers but it didn't work, first I've registered the event using the += notation as follows: 我已经设法通过使用EventHandler来检索当前LocationRect中心的坐标,该EventHandler与按下箭头相关联。我尝试通过鼠标单击事件处理程序使用相同的概念,但没有用,首先我注册了该事件使用+ =表示法如下:

public MainWindow()
        {
            InitializeComponent();
            MainMap.Mode = new AerialMode(true);
            MainMap.Focus();
            MainMap.Culture = "ar-sa";

            MainMap.MouseDoubleClick += new MouseButtonEventHandler(MapWithPushpins_MouseDoubleClick);
        }

private void MapWithPushpins_MouseDoubleClick(object sender,  MouseButtonEventArgs e)
    {
        e.Handled = true;

        Point mousePosition = e.GetPosition(this);
        Location pinLocation = MainMap.ViewportPointToLocation(mousePosition);

        Pushpin pin = new Pushpin();
        pin.Location = pinLocation;


        Coordinates.Text = pinLocation.Longitude.ToString();
        MainMap.Children.Add(pin);

    }   

And here's the XAML file: 这是XAML文件:

<Window x:Class="Ornina.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:m="clr-namespace:Microsoft.Maps.MapControl.WPF;assembly=Microsoft.Maps.MapControl.WPF"
        xmlns:local="clr-namespace:Ornina"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid VerticalAlignment="Top" >
            <m:Map CredentialsProvider="AqCitpgSjIz_Sxd6AyI9Zm1rs1uRSG_G3Y7ebfok69ufB8W8uRdUtvheaRbz_10t" x:Name="MainMap" Center="36,38" ZoomLevel="16" Mode="AerialWithLabels" HorizontalAlignment="Center" VerticalAlignment="Top" Height="300" Width="500">
            </m:Map>
        </Grid>
        <TextBlock x:Name="Coordinates">Coordinations</TextBlock>
    </Grid>
</Window>

The program isn't responding with any thing, no exceptions, no errors 该程序没有任何响应,没有异常,没有错误

Your TextBlock is in front of your map, so the map doesn't receive the MouseDoubleClick event. 您的TextBlock位于地图的前面,因此地图没有收到MouseDoubleClick事件。

You can change the TextBlock to: 您可以将TextBlock更改为:

  <TextBlock x:Name="Coordinates"
             HorizontalAlignment="Left"
             VerticalAlignment="Top"
             Text="Coordinations" />

So it's only in the top left corner and not in front of the whole map. 因此,它仅位于左上角,而不位于整个地图的前面。

Or you can move it outside of the map entirely, in a different grid row or column. 或者,您可以将其完全移动到地图之外的其他网格行或列中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM