MapControl get tapped location UWP

最后都变了- 提交于 2019-12-12 03:44:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!