Add a Simple PushPin with Bing Maps in a Windows Phone 7

Introduction

This is a simple application for beginners to use to develop Windows Phone 7 Tools that help to create a pushpin a in website map. Place base Pushpin provides an easy to use JavaScript Application Programming Interface for embedding scrollable maps into your website. Windows Mobile 7 development is done using the .NET framework. The .NET framework is a software framework created by Microsoft for use in creating Windows applications. In this article you will learn how to create a pushpin in your Windows Phone application.

Step 1 : Open Visual Studio.

  • Select new -> project
  • Select your preferred language
  • Select the silverlight for Windows Phone application
  • Name the project
  • Now name of project is "Tom application"

p3.gif

123445.gif

tom 2.gif


Step  2 : 
Open the Toolbox of the Windows Phone application.

  • Drag & Drop map tools on design view.

map drag and drop.gif


Step 3 :
Provide a credential key on map xaml code. The following steps create a credential key:

  • First create a account on HotMail
  • After that register this site: link
  • Finally generate your credential key
  • just like a : "ApXjYPChA-MW0fszWs_Djhgsfdjhdfhdf-Ov1zjkhjk_1Bm--hj9I56Qr9UuLAa"

rararara.gif


Step 4 :
Set the some formatting on map xaml code.

Code

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <my:Map Height="498"
             CredentialsProvider
="ApXjYPChA-MW0fszWs_DjwbGzJsJU2V7-Ov1zhqwZl39_1Bm--hj9I56Qr9UuLAa"
            HorizontalAlignment="Left" Margin="0,6,0,0" Name="map1" VerticalAlignment="Top" Width="468">
            <my:MapItemsControl x:Name="mapControl" />
          </my:Map>
       <Button Content="Location" Height="72" HorizontalAlignment="Left" Margin="121,510,0,0" Name="button1" VerticalAlignment="Top"
     Width="206" Click="button1_Click" Foreground="White" Background="#FFB81B1B"></Button>
 </Grid>

 

Step 5 : The final presentation of design view is given below:

Code

<phone:PhoneApplicationPage
    x:Class="WindowsPhoneApplication2.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" 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"
    d:DesignHeight="768" d:DesignWidth
="480">
    <phone:PhoneApplicationPage.Resources>
        <ControlTemplate x:Key="pinMyLoc" TargetType="my:Pushpin">
            <Grid Height="26" Width="26" Margin="-13,-13,0,0" RenderTransformOrigin="0.5,0.5">
                <Grid.RenderTransform>
                    <CompositeTransform Rotation="-45"/>
                </Grid.RenderTransform>
                <Rectangle Fill="Red" HorizontalAlignment="Center"          
                Margin
="0" Stroke="White" VerticalAlignment="Center"        
                 Height="26" Width
="26"/>
                <Ellipse HorizontalAlignment="Center" Height="16" Margin="0"          
                 VerticalAlignment
="Center" Fill="Yellow"
                 Width
="16"/>
                 </Grid>
                 </ControlTemplate>
                  </phone:PhoneApplicationPage.Resources>
                    <!--LayoutRoot is the root grid where all page content is placed-->
                  <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="Tom Application" Style="{StaticResource PhoneTextNormalStyle
              "Foreground="#FFFF50FF" />
            <TextBlock x:Name="PageTitle" Text="Bing Map" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style
           "OpacityMask="#FFFA0000" Foreground="#FF2ADE78" />
         </StackPanel>
       <!--ContentPanel - place additional content here-->
      <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
      <my:Map Height="498" CredentialsProvider="ApXjYPChA-MW0fszWs_DjwbGzJsJU2V7-Ov1zhqwZl39_1Bm--hj9I56Qr9UuLAa"
     HorizontalAlignment="Left" Margin="0,6,0,0" Name="map1" VerticalAlignment="Top" Width="468">
    <my:MapItemsControl x:Name="mapControl" />
  </my:Map>
 <Button Content="Location" Height="72" HorizontalAlignment="Left" Margin="121,510,0,0" Name="button1" VerticalAlignment="Top"
Width="206" Click="button1_Click" Foreground="White" Background="#FFB81B1B"></Button></Grid>
</
Grid>
</
phone:PhoneApplicationPage>

final design.gif


Step 6 : 
Write the code of the Location button.

Code

private void button1_Click(object sender, RoutedEventArgs e)
        {
           if (man == null)
            {
                man = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
                man.StatusChanged += loc_StatusChanged;
            }
            if (man.Status == GeoPositionStatus.Disabled)
            {
                man.StatusChanged -= loc_StatusChanged;
                MessageBox.Show("Location service install in your phone");
                return;
            }
            man.Start();
        }

Step 7 : Write the code of the allocated pushpin location.

Code

void loc_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
        {
            if (e.Status == GeoPositionStatus.Ready)
            {
                Pushpin pin = new Pushpin();
                pin.Template = this.Resources["pinMyLoc"] as ControlTemplate;
                pin.Location = man.Position.Location;
                mapControl.Items.Add(pin);
                map1.SetView(man.Position.Location, 15.0);
                man.Stop();
            }
        }

Step 8 : Write the final code of the pushpin allocation of the maninpage.aspx.cs page.

Code

using
System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Device.Location;
using Microsoft.Phone.Controls.Maps;
namespace TomApplication
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            map1.ZoomBarVisibility = System.Windows.Visibility.Visible;  
            map1.LogoVisibility = Visibility.Collapsed; 
            map1.CopyrightVisibility = Visibility.Collapsed; 
            map1.Mode = new AerialMode();
        }
        private GeoCoordinateWatcher man = null;
        private void button1_Click(object sender, RoutedEventArgs e)
        {
           if (man == null)
            {
                man = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
                man.StatusChanged += loc_StatusChanged;
            }
            if (man.Status == GeoPositionStatus.Disabled)
            {
                man.StatusChanged -= loc_StatusChanged;
                MessageBox.Show("Location service install in your phone");
                return;
            }

            man.Start();
        }
        void loc_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
        {
            if (e.Status == GeoPositionStatus.Ready)
            {
                Pushpin pin = new Pushpin();
                pin.Template = this.Resources["pinMyLoc"] as ControlTemplate;
                pin.Location = man.Position.Location;
                mapControl.Items.Add(pin);
                map1.SetView(man.Position.Location, 15.0);
                man.Stop();
            }
        }
    }
}

Step 9 : Press Crtl + F5 to run application.

Output

 out1.gif

 out 2.gif

 final design.gif

 out 4.gif


Resource


Similar Articles