Introduction
In this article, we will discuss how to add a Pushpin in our Bing Maps and how we can change the image of the Pushpin Icon.
Step 1: First we should add the following references into our program.
using Microsoft.Phone.Controls.Maps;
using System.Device.Location;
Step2: After that in the .xaml page we add this line in the <phone:PhoneApplicationPage> tag.
<phone:PhoneApplicationPage
x:Class="WindowsPhoneApplication9.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True"
xmlns:my="clr-namespace:Microsoft.Phone.Controls.Maps;assembly=Microsoft.Phone.Controls.Maps">
It is used to declare the map.
Step 3: Now we add a Map with two Buttons (one is used to add the pin in the center of the map and the second is used to change the image of the pin).
<my:Map Height="427" HorizontalAlignment="Left" Margin="12,6,0,0" Name="map1" VerticalAlignment="Top" Width="438" />
<Button Content="Add Center Pushpin" Height="72" HorizontalAlignment="Left" Margin="0,467,0,0" Name="button1" VerticalAlignment="Top" Width="307" Click="button1_Click" />
<Button Content="Chage Pushpin Image" Height="72" HorizontalAlignment="Left" Margin="68,529,0,0" Name="button2" VerticalAlignment="Top" Width="302" Click="button2_Click" />
The Complete Grid Coding is:
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<Grid x:Name="ContentPanel" Margin="12,160,12,1" Grid.RowSpan="2">
<my:Map Height="427" HorizontalAlignment="Left" Margin="12,6,0,0" Name="map1" VerticalAlignment="Top" Width="438" />
<Button Content="Add Center Pushpin" Height="72" HorizontalAlignment="Left" Margin="0,467,0,0" Name="button1" VerticalAlignment="Top" Width="307" Click="button1_Click" />
<Button Content="Chage Pushpin Image" Height="72" HorizontalAlignment="Left" Margin="68,529,0,0" Name="button2" VerticalAlignment="Top" Width="302" Click="button2_Click" />
</Grid>
</Grid>
OUTPUT
Step 4: Now we start the code in the .cs page. First we write the code for Button1 (which is used to add the Center Pushpin).
First we create a GeoCoordiate like this:
private GeoCoordinate mapCenter;
After that we start the coding for Button1:
private void button1_Click(object sender, RoutedEventArgs e)
{
mapCenter = map1.Center;
Pushpin mypin = new Pushpin();
mypin.Location = mapCenter;
map1.Children.Add(mypin);
}
Here the following code is used to get the center of the current map:
mapCenter = map1.Center;
and the rest code is used to add the pin in the center of the map.
The ouput will be:
Step 5: Now we write the code for changing the image of the Pin:
For this first we add an ImageLayer into our program:
MapLayer imgLayer;
// Constructor
public MainPage()
{
InitializeComponent();
imgLayer = new MapLayer();
map1.Children.Add(imgLayer);
}
Then we write the following code:
private void button2_Click(object sender, RoutedEventArgs e)
{
mapCenter = map1.Center;
Image pinIMG = new Image();
pinIMG.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("mypin.png", UriKind.Relative));
pinIMG.Height = 50;
pinIMG.Width = 50;
pinIMG.Stretch = System.Windows.Media.Stretch.None;
PositionOrigin position = PositionOrigin.Center;
imgLayer.AddChild(pinIMG, mapCenter, position);
}
The Output Will be: