I'm making a Windows Store app using C# and need a bit of help with a method for when a pushpin on a map is tapped. So far I create a Pushpin called currentPin that I create in the XAML code. I also make a reference for the Tapped event here called pushpin_Tapped.
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<bm:Map x:Name="MyMap" Credentials="{StaticResource BingMapsApiKey}" Holding="map_Held">
<bm:Map.Children>
<bm:Pushpin x:Name="pin" Tapped="pushpinTapped">
</bm:Pushpin>
</bm:Map.Children>
</bm:Map>
</Grid>
I then place the currentPin on my current location in the OnNavigatedTo method in the MainPage.xaml.cs code. I also create the method for when this pushpin is tapped. In this method I show a dialog box for when the currentPin is tapped.
private async void pushpinTapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e)
{
var x = MapLayer.GetPosition(pin);
MessageDialog dialog = new MessageDialog("You are here " + x.Latitude + " " + x.Longitude);
await dialog.ShowAsync();
}
I then have a method that place a new pushpin on a location when the map is held by the user, code below:
private void map_Held(object sender, HoldingRoutedEventArgs e)
{
Debug.WriteLine("You held at" + DateTime.Now.ToString() + "" + e.GetPosition(MyMap));
var pos = e.GetPosition(MyMap);
Location location;
MyMap.TryPixelToLocation(pos, out location);
Pushpin pin = new Pushpin();
MyMap.Children.Add(pin);
MapLayer.SetPosition(pin, location);
}
I know it's probably staring me in the face but I'm blanking on how to get a message box or dialog box or anything to happen when each of these new pins is tapped. Could anyone shed some light please? Thanks, Aimee
In Map_Held, add the handler for the tapped event...
private void map_Held(object sender, HoldingRoutedEventArgs e)
{
Debug.WriteLine("You held at" + DateTime.Now.ToString() + "" + e.GetPosition(MyMap));
var pos = e.GetPosition(MyMap);
Location location;
MyMap.TryPixelToLocation(pos, out location);
Pushpin newpin = new Pushpin();
newpin.Tapped += pushpin_Tapped;
MyMap.Children.Add(newpin);
MapLayer.SetPosition(newpin, location);
}
and in your tapped event handler, change the reference to your pin from pin
to the following.
private async void pushpinTapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e)
{
PushPin tappedpin = sender as PushPin; // gets the pin that was tapped
if(null == tappedpin) return; // null check to prevent bad stuff if it wasn't a pin.
var x = MapLayer.GetPosition(tappedpin);
MessageDialog dialog = new MessageDialog("You are here " + x.Latitude + " " + x.Longitude);
await dialog.ShowAsync();
}