user2915962
user2915962

Reputation: 2711

How do I trigger click event to get the coordinates from the map in uiversal app

I trying to perform a seeimly simple task in a universal App. I want to be able to click on my map and have that click trigger an event which will give me the coordinates.

There are many guides demonstrating how to do this but I need a solution that will work in my universal app (windows 8.1/WindowsPhone).

MSDN-blogger Ricky Brundritt have written my great posts on the subject and I have followed this guide in order to create a map that can be used in both projects.

http://blogs.msdn.com/b/rbrundritt/archive/2014/06/24/how-to-make-use-of-maps-in-universal-apps.aspx

If I understand correctly he creates a file with conditional statements that gets shared between the projects. And when you call the method the proper implementatoin gets used depending on which project is run:

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
        }

Now, is there a way to implement a method that will give me the coordinates when i Click on a map? Here is an answer to a similar question: (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);
}

But this will not work for me beacuse cant get acess to;

this.MyMap.PointerPressedOverride += MyMap_PointerPressedOverride;

I only have:

this.MyMap.PointerPressed += MyMap_PointerPressedOverride;

I also dont have acess to TryPixelToLocation in

 this.MyMap.TryPixelToLocation(e.GetCurrentPoint(this.MyMap).Position, out l);

SO to sum it up, im looking for a way to trigger an event when i click on the map that will work in both projects. Help appreciated.

Thank you!

EDIT:

#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

I have a strange problem with getting this to work on the phone. The method does not fire when i push on the map. When debugging on a device, you can see som numbers at the side of the screen. When I push where the numbers are the method fires and a gets added "behind" the numbers. This is really strange. Somehow the "numbers" are clickable but the map itself is not.

Have anyone had similar problems?

Upvotes: 0

Views: 1487

Answers (1)

rbrundritt
rbrundritt

Reputation: 17954

You can create an event handler in the map view control that returns the coordinate of where a user clicked on the map. You can then write the required code for the different map controls for handing the click events, getting the map coordinate, and passing it on to your event handler. Here is a modified constructor for the MapView control and some additional code that adds a MapClicked event and code that wraps the map controls and passes the map coordinate to the event.

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

Upvotes: 1

Related Questions