Bing Maps Task In Windows Phone 8

Introduction

This article explains the process of launching Bing Maps from an app. We will see how to provide access to a map from our app to the user. For that purpose we will use a Windows Phone task launcher. So let's start.

Bing Maps Task

This is a kind of launcher provided by Windows Phone to launch Bing Maps. It helps in easy integration of Bing Maps app with other apps. It enables the user to visualize the actual place.

Before using this task we need to include the following namespace:

using Microsoft.Phone.Tasks;

This task has the following properties that can be set:

  • Search Term
    Term to be searched in Bing Maps.
     
  • Center
    Center point of the Map. It's an initial location when no search is done.
     
  • Zoom level
    Initial zoom level of the map.
     

The task requires a search term to be non-empty. In case of empty search term, your app may crash.

Demo

XAML

<phone:PhoneApplicationPage

    x:Class="Demo.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">

 

    <!--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 Text="Demo" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>

            <TextBlock  Text="Demo" Margin="9,-7,0,0" FontSize="40" />

        </StackPanel>

 

        <!--ContentPanel - place additional content here-->

        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

            <StackPanel Orientation="Vertical">

                <TextBox Name="srchtxt"></TextBox>

                <Button Name="srchBtn" Click="srch" Content="Search in Bing MAP" HorizontalAlignment="Left" Width="446" Height="129" VerticalAlignment="Bottom"/>

                <Button Name="srchBtnCenter" Click="srchCtr" Content="Search in Bing MAP(Center)" HorizontalAlignment="Left" Width="446" Height="129" VerticalAlignment="Bottom"/>

                <Button Name="srchBtnZoom" Click="srchZoom" Content="Search in Bing MAP(Zoom)" HorizontalAlignment="Left" Width="446" Height="129" VerticalAlignment="Bottom"/>

            </StackPanel>

        </Grid>

    </Grid>

</phone:PhoneApplicationPage>

C# Code Behind

using Microsoft.Phone.Controls;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using Windows.Phone.Speech.Synthesis;

using Windows.Devices.Sensors;

using System.Diagnostics;

using Microsoft.Phone.Net.NetworkInformation;

using Microsoft.Phone.Tasks;

 

namespace Demo

{

    public partial class MainPage : PhoneApplicationPage

    {

        // Constructor

        public MainPage()

        {

            InitializeComponent();

        }

 

        private void srch(object sender, RoutedEventArgs e)

        {

            /* Step 1 */

            Microsoft.Phone.Tasks.BingMapsTask bmt = new BingMapsTask();

            /* Step 2 */

            bmt.SearchTerm = srchtxt.Text;

            /* Step 3 */

            bmt.Show();  

        }

 

        private void srchCtr(object sender, RoutedEventArgs e)

        {

            /* Step 1 */

            Microsoft.Phone.Tasks.BingMapsTask bmt = new BingMapsTask();

            /* Step 2 */

//            bmt.SearchTerm = srchtxt.Text;

            bmt.Center = new System.Device.Location.GeoCoordinate() { Latitude = 27.98582, Longitude = 86.92360};

            /* Step 3 */

            bmt.Show();  

 

        }

 

        private void srchZoom(object sender, RoutedEventArgs e)

        {

            /* Step 1 */

            Microsoft.Phone.Tasks.BingMapsTask bmt = new BingMapsTask();

            /* Step 2 */

            bmt.SearchTerm = srchtxt.Text;

            bmt.ZoomLevel = 1.0;

            /* Step 3 */

            bmt.Show();  

        }

    }

}

The Show method is used to launch the map task.

Output


 

 

 

Up Next
    Ebook Download
    View all
    Learn
    View all