Joseph Roberts
Joseph Roberts

Reputation: 569

WPF Bing Maps places push pin at incorrect position

I have followed the tutorial at https://msdn.microsoft.com/en-GB/library/hh709044.aspx to learn how to place pins on the map.

It all works apart from that the pins are consistently being placed about 3cm below my mouse pointer.

I'm not sure what I'm doing wrong.

Heres my xaml code

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

and my click handler

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

Any ideas why my pin is not being placed in the correct position?

Thanks Joe

Upvotes: 1

Views: 1653

Answers (1)

Clemens
Clemens

Reputation: 128013

You'll have to get the viewport point relative to the Map control, not the Window or UserControl that declares the MapWithPushpins_MouseDoubleClick method.

Change the parameter passed to GetPosition from this to Map:

var mousePosition = e.GetPosition(Map);

Or to the sender parameter, which also references the Map control:

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

Upvotes: 4

Related Questions