Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create MapIcon event in UWP?

Tags:

c#

maps

xaml

uwp

I want to add pushpins to my MapControl that can be tapped. Since Windows 8.1 pushpin class is no longer available and UWP offers us something called ImageIcon(imo its kinda crappy). Here's my code:

BasicGeoposition bg = new BasicGeoposition() { Latitude = 52.280, Longitude = 20.972 };
Geopoint snPoint = new Geopoint(bg);
MapIcon mapIcon1 = new MapIcon();
mapIcon1.Location = snPoint;
mapIcon1.NormalizedAnchorPoint = new Point(0.5, 1.0);
MyMap.MapElements.Add(mapIcon1);

How can I make it event handling(like tap or click)?

Thank you in advance

like image 387
Pawel Sienkiewicz Avatar asked Dec 20 '15 01:12

Pawel Sienkiewicz


1 Answers

In UWP you can put much more elements in the map and click event is handled little different way - take a look at MapControl.MapElementClick. Events are handled by MapControl - so you don't need to subscribe in every map's element - the mentioned event will return list of clicked elements. The sample code can look like this:

<map:MapControl Name="MyMap" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" MapElementClick="MyMap_MapElementClick"/>
private void MyMap_MapElementClick(Windows.UI.Xaml.Controls.Maps.MapControl sender, Windows.UI.Xaml.Controls.Maps.MapElementClickEventArgs args)
{
    MapIcon myClickedIcon = args.MapElements.FirstOrDefault(x => x is MapIcon) as MapIcon;
    // do rest
}
like image 198
Romasz Avatar answered Oct 16 '22 17:10

Romasz