简体   繁体   中英

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

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:

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:

<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.

You can change the TextBlock to:

  <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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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