Over a million developers have joined DZone.

How-To: Play With Bing Maps Task in Windows Phone

In this short tutorial we are going to see how to use a Bing Maps task in a Windows Phone application. With this task we can launch the Bing Maps application and provide a location string to search and get the maps on to the specific location. If any search location is not specified by the user then Bing Map task will take the present location of the user. Bing maps task provides option to set the zoom level to a specified level which can also be set programmatically. Let us see the steps on how to achieve this task in real time for a Windows phone application.

Open Visual Studio 2010 IDE and create a new Silverlight for Windows Phone project with a valid project name as shown in the screen below. Once the project is created add some controls which are used to trigger the Bing Maps Task as shown in the screen below.

image

Now drag and drop few controls to the screen which is used to trigger event that launches the Bing Maps Task. Once we designed the screen with the controls we can see the screen looks like below.

image

Now we need to write our code in the button click event to trigger the Bing Map launcher task on the user click. To do that just go to the code behind and first add the USING handler code on top with the existing using statements as shown in the code below.

using Microsoft.Phone.Tasks;

using System.Device.Location;

For System.Device.Location we need to manually add reference to System.Device Name space.

Next is to add the below code to the button click event which basically creates an instance of the Bing Maps task as shown in the code below. Note that if we are going to target a 256Mb ram device we can use the Bing Maps task instead of Map control which utilizes minimum Memory allocation that is used in the application.

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 Microsoft.Phone.Tasks;
using System.Device.Location;

namespace F5debugHowto17
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}

private void button1_Click(object sender, RoutedEventArgs e)
{
BingMapsTask bingMapsTask = new BingMapsTask();
bingMapsTask.Center = new GeoCoordinate(0, 0);
bingMapsTask.ZoomLevel = 2;
bingMapsTask.Show();
}
}
}

image

With this Bing Maps Task we can set properties based on the requirement like we have property to search based on some keyword or can be searched with latitude and longitude using the GeoCoordinates for a particular location. Bing Maps tasks has below properties which can be used to play around as per the requirement.

S No Property Name Description
1 Center use GeoCoordinate to specify the location to which it need to point to
2 SearchTerm can be provided with some search terms to search for location e.g. hotels, hospitals, etc
3 ZoomLevel can be provided with the initial zoom level desired for the user

 

Now we are done with our code, just run the application by pressing F5 directly from the keyboard or we can use the Build and execute the project option from the tool bar to run the application. Once the Build is successful we can see the Windows Phone emulator with the application and the expected outputs as shown in the screens below.

Output Screens:

image

So in this tutorial we have seen how to use Bing Maps task with the different options that are available. That’s it from this short tutorial on Windows Phone see you all in the next tutorial soon. Mean while Happy Programming!!!

 

 

Topics:

Published at DZone with permission of Karthikeyan Anbarasan, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}