0

I have a MapControl in my app and I want to retrieve the coordinate of the point taped by the user.

<Maps:MapControl    Grid.Row="0" 
                    ColorScheme="Light" 
                    Margin="10" 
                    x:Name="mainMap" 
                    HorizontalAlignment="Stretch"
                    VerticalAlignment="Stretch"
                    Tapped="mainMap_Tapped"
                    MapElementClick="mainMap_MapElementClick"
                />

But I don't know how to get this from the event private void mainMap_Tapped(object sender, TappedRoutedEventArgs e)

Jay Zuo
  • 15,653
  • 2
  • 25
  • 49
Sven Borden
  • 1,070
  • 1
  • 13
  • 29

2 Answers2

3

To get the tapped location in MapControl, we can use MapControl.MapTapped event. This event occurs when the user taps the MapControl or clicks on it with the left mouse button. An instance of MapInputEventArgs provides data for this event. And in MapInputEventArgs, we can get the location with MapInputEventArgs.Location property. For example:

In XAML:

<Maps:MapControl x:Name="mainMap"
                 Grid.Row="0"
                 Margin="10"
                 HorizontalAlignment="Stretch"
                 VerticalAlignment="Stretch"
                 ColorScheme="Light"
                 MapTapped="mainMap_MapTapped"
                 MapElementClick="mainMap_MapElementClick" />

In code-behind:

private void mainMap_MapTapped(Windows.UI.Xaml.Controls.Maps.MapControl sender, Windows.UI.Xaml.Controls.Maps.MapInputEventArgs args)
{
    var tappedGeoPosition = args.Location.Position;
    string status = "MapTapped at \nLatitude:" + tappedGeoPosition.Latitude + "\nLongitude: " + tappedGeoPosition.Longitude;
    rootPage.NotifyUser( status, NotifyType.StatusMessage);
}
Jay Zuo
  • 15,653
  • 2
  • 25
  • 49
1
GeoPoint geoPt = this.mainMap.Layers[0].ScreenToGeoPoint(e.GetPosition(this.mapControl1)); 

Should get you the geopoint.

Jared Kove
  • 235
  • 1
  • 13
  • I got the `Layers[0]` underlined saying that there is no method – Sven Borden Jul 27 '16 at 16:18
  • @SvenBorden That would be because you used parenthesis "( )" instead of brackets "[ ]", meaning you tried to call a method instead of reference a index – Jared Kove Jul 27 '16 at 16:27
  • it also says that no definition for "Layers" and no method extension "Layers" accepts a first argument of type MapControl could be found (btw thanks a lot) – Sven Borden Jul 27 '16 at 16:48