如何触发点击事件以从通用应用程序中的地图获取坐标

时间:2014-12-06 07:30:12

标签: c# bing-maps

我试图在通用应用程序中执行一个看似简单的任务。 我希望能够点击我的地图并点击该按钮触发一个事件,它将为我提供坐标。

有许多指南演示如何执行此操作,但我需要一个可在我的通用应用程序(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

如果我理解正确,他会创建一个带有条件语句的文件,这些语句可以在项目之间共享。当您调用该方法时,将根据运行的项目使用正确的implementsato:

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
        }

现在,有没有办法实现一种方法,当我点击地图时会给我坐标? 以下是对类似问题的回答:(how to get the geolocation of a click on a bing map in 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编号。当我按下数字的方法时,方法会触发,并且数字会“添加”。这真的很奇怪。不知何故,“数字”是可点击的,但地图本身不是。

有没有人有类似的问题?

1 个答案:

答案 0 :(得分:1)

您可以在地图视图控件中创建一个事件处理程序,它返回用户在地图上单击的坐标。然后,您可以为不同的地图控件编写所需的代码,以处理单击事件,获取地图坐标,并将其传递给事件处理程序。这是一个用于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
相关问题