问题
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)
回答1:
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);
}
回答2:
GeoPoint geoPt = this.mainMap.Layers[0].ScreenToGeoPoint(e.GetPosition(this.mapControl1));
Should get you the geopoint.
来源:https://stackoverflow.com/questions/38617385/mapcontrol-get-tapped-location-uwp