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

问题描述 投票:0回答:1

我正在使用Bing地图WPF控件SDK来尝试检索坐标并打印它们

我已经设法使用与按下箭头相关联的EventHandler检索当前LocationRect中心的坐标我尝试使用鼠标单击事件处理程序使用相同的概念但是它没有用,首先我注册了事件使用+ =表示法如下:

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

    }   

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

该程序没有响应任何事情,没有例外,没有错误

c# bing-maps
1个回答
1
投票

您的TextBlock位于地图前面,因此地图不会收到MouseDoubleClick事件。

您可以将TextBlock更改为:

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

所以它只在左上角而不是在整个地图的前面。

或者,您可以将其完全移出地图,位于不同的网格行或列中。

© www.soinside.com 2019 - 2024. All rights reserved.