如何触发点击事件以从 uiversal 应用程序中的地图中获取坐标
本文关键字:应用程序 地图 坐标 获取 uiversal 何触发 事件 | 更新日期: 2023-09-27 18:33:28
我试图在通用应用程序中执行一个看似简单的任务。我希望能够单击我的地图,并让该单击触发一个事件,该事件将为我提供坐标。
有许多指南演示如何执行此操作,但我需要一个可以在我的通用应用程序(Windows 8.1/WindowsPhone(中使用的解决方案。
MSDN 博主 Ricky Brundritt 写了关于这个主题的精彩文章,我按照本指南创建了一个可以在两个项目中使用的地图。
http://blogs.msdn.com/b/rbrundritt/archive/2014/06/24/how-to-make-use-of-maps-in-universal-apps.aspx
如果我理解正确,他会创建一个带有条件语句的文件,该文件在项目之间共享。当你调用该方法时,会根据运行的项目使用正确的实现:
E.x:
public void SetView(BasicGeoposition center, double zoom)
{
#if WINDOWS_APP
_map.SetView(center.ToLocation(), zoom);
OnPropertyChanged("Center");
OnPropertyChanged("Zoom");
#elif WINDOWS_PHONE_APP
_map.Center = new Geopoint(center);
_map.ZoomLevel = zoom;
#endif
}
现在,有没有办法实现一种方法,当我点击地图时给我坐标?以下是类似问题的答案:(如何在 C# 中获取单击必应地图的地理位置(
public MainPage()
{
this.InitializeComponent();
this.MyMap.PointerPressedOverride += MyMap_PointerPressedOverride;
}
void MyMap_PointerPressedOverride(object sender, PointerRoutedEventArgs e)
{
Bing.Maps.Location l = new Bing.Maps.Location();
this.MyMap.TryPixelToLocation(e.GetCurrentPoint(this.MyMap).Position, out l);
Bing.Maps.Pushpin pushpin = new Bing.Maps.Pushpin();
pushpin.SetValue(Bing.Maps.MapLayer.PositionProperty, l);
this.MyMap.Children.Add(pushpin);
}
但这对我不起作用,因为无法获得
;this.MyMap.PointerPressedOverride += MyMap_PointerPressedOverride;
我只有:
this.MyMap.PointerPressed += MyMap_PointerPressedOverride;
我也没有TryPixelToLocation
this.MyMap.TryPixelToLocation(e.GetCurrentPoint(this.MyMap).Position, out l);
所以总结一下,当我单击将在两个项目中工作的地图时,我正在寻找一种触发事件的方法。感谢帮助。
谢谢!
编辑:
#elif WINDOWS_PHONE_APP
private void _map_PointerPressed(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
{
Geopoint p;
_map.GetLocationFromOffset(e.GetCurrentPoint(_map).Position, out p);
MapClicked(p);
}
#endif
我在电话上使用它有一个奇怪的问题。当我在地图上推送时,该方法不会触发。在设备上调试时,可以在屏幕一侧看到 som 数字。当我按下数字的位置时,方法会触发,并在数字"后面"添加 a。这真的很奇怪。不知何故,"数字"是可点击的,但地图本身不是。
有人有类似的问题吗?
可以在地图视图控件中创建事件处理程序,该处理程序返回用户在地图上单击的位置的坐标。然后,可以为不同的映射控件编写所需的代码,以便处理单击事件、获取映射坐标并将其传递给事件处理程序。下面是 MapView 控件的修改构造函数和一些附加代码,用于添加 MapClicked 事件和包装地图控件并将地图坐标传递给事件的代码。
public MapView()
{
#if WINDOWS_APP
_map = new Map();
_shapeLayer = new MapShapeLayer();
_map.ShapeLayers.Add(_shapeLayer);
_pinLayer = new MapLayer();
_map.Children.Add(_pinLayer);
_map.PointerPressedOverride += _map_PointerPressedOverride;
#elif WINDOWS_PHONE_APP
_map = new MapControl();
_map.PointerPressed += _map_PointerPressed;
#endif
this.Children.Add(_map);
SetMapBindings();
}
public delegate void MapClickHandler(Geopoint center);
/// <summary>
/// A callback method used to when the map is Clicked
/// </summary>
public event MapClickHandler MapClicked;
#if WINDOWS_APP
private void _map_PointerPressedOverride(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
{
Location l;
_map.TryPixelToLocation(e.GetCurrentPoint(_map).Position, out l);
Geopoint p = new Geopoint(new BasicGeoposition()
{
Latitude = l.Latitude,
Longitude = l.Longitude
});
MapClicked(p);
}
#elif WINDOWS_PHONE_APP
private void _map_PointerPressed(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
{
Geopoint p;
_map.GetLocationFromOffset(e.GetCurrentPoint(_map).Position, out p);
MapClicked(p);
}
#endif