A blog focused on developing applications with Bing Maps

Menu

Geocoding and Routing in Bing Maps Windows Store Apps (Native)

In this blog post we are going to look at how implement Geocoding and Routing using the Native Bing Maps Windows Store App SDK. If you are new to developing with the Native Bing Maps Windows Store App SDK I recommend reading the Getting started with Bing Maps Windows Store Apps blog post first. We will also be making use of the Bing Maps REST Services, if you are unfamiliar with using this in native code take a look at this MSDN documentation on Using the REST services with .NET. The methods covered in this blog post can be used as an example of how to access other REST services from Windows Store applications.

Geocoding is one of the most common tasks by users of online maps. Geocoding is the process of taking an address or query and returning its equivalent coordinate on the map. Routing is the task of calculating the directions between two or more locations. In Bing Maps, there are a lot of different options around routing such as routing by different modes of transportation; driving, walking, or transit.

Setting up the project

To set up the project, open Visual Studios 2012 and create a new project. In the window that opens, select Visual C# -> Windows Store. Select the Blank App template and call the application BingMapsSearch_WinRT_CS and press OK.

Next, we will add in the libraries needed for serializing the response from the Bing Maps REST services. To do this, right-click on the project and select Add -> New Item. Create a class file called BingMapsRESTServices.cs. Open up this class file and delete the contents. Go to the MSDN documentation on Using the REST services with .NET and copy and paste the C# Data Contracts into this file. At this point, your Solution should look something like this:

Adding Bing Maps to the App

To get started we will need to add a reference to the Bing Maps SDK. To do this, right click on the References folder and press Add Reference. Select Windows -> Extensions select Bing Maps for C#, C++ and Visual Basic. If you do not see this option, take a moment to verify that you have installed the Bing Maps SDK for Windows 8 style apps. While you are here, you should add a reference to the Microsoft Visual C++ Runtime Package as it is required by the Bing Maps SDK when developing using C# or Visual Basic.

You may notice that there is a little yellow indicator on the references that you just added. This is because the Bing Maps SDK requires that you set the Active solution platform in Visual Studio to one of the following options.

> C#, Visual Basic: ARM, x86 or x64

> C++: ARM, Win32 or x64

To do this, right click on the Solution folder and select Properties. Then go to Configuration Properties -> Configuration. Find your project and under the Platform column set the target platform. For this blog post I’m going to select x86. Press Ok and the yellow indicator should disappear from our references.

Now we can add a map to our application. To do this, open the MainPage.xaml file. We will first need to add the Bing Maps SDK as a namespace at the top of the file. After we do this we can add a Map object to the Grid control and add our Bing Maps key to the credentials properties of the map. We will also give the map a name of MyMap. While we are at it, we will create a side panel that has input controls for geocoding and routing and a button for clearing the map. In the side panel we will also add two ListBox objects. We will use these to display the geocode results and route itinerary to the user. The XAML for file will look like this:

If you create the event handlers for the buttons and the GeocodeResultsListBox you can then run the application to see what it looks like. You should end up with something like this:

Adding the Application Logic

Open up the MainPage.xaml.cs file. In here we will create a global MapShapeLayer variable called routeLayer inside the MainPage class. We will use this shape layer to display the route line on the map. In the constructor of the MainPage class we will initialize this variable and add it to the map. At this point, the MainPage.xaml.cs file will look like this:

using Bing.Maps;

using BingMapsRESTService.Common.JSON;

using System;

using System.Runtime.Serialization.Json;

using System.Threading.Tasks;

using Windows.UI;

using Windows.UI.Popups;

using Windows.UI.Xaml;

using Windows.UI.Xaml.Controls;

using Windows.UI.Xaml.Media;

using Windows.UI.Xaml.Navigation;

namespace BingMapsSearch_WinRT_CS

{

publicsealedpartialclassMainPage : Page

{

privateMapShapeLayer routeLayer;

public MainPage()

{

this.InitializeComponent();

routeLayer = newMapShapeLayer();

MyMap.ShapeLayers.Add(routeLayer);

}

protectedoverridevoid OnNavigatedTo(NavigationEventArgs e)

{

}

}

}

There are a few other common methods that we will want to add to this file to make things a bit easier and keep the code clean. The first common task we will want to do is notify the user if there is an error with their request. In .NET we would normally use the MessageBox class, but this is not available to Windows Store applications. Instead, we have to use the MessageDialog class. This class has a lot more options than we need, so to keep things simple, we will create a method that takes in a string message and displays it to the user. We will call this method ShowMessage and use the following code:

privateasyncvoid ShowMessage(string message)

{

MessageDialog dialog = newMessageDialog(message);

await dialog.ShowAsync();

}

The second common task is to call the Bing Maps REST service and then serialize the response into a BingMapsRESTService.Common.JSON.Response object. This class comes from the data contracts we copied in from MSDN documentation on Using the REST services with .NET. In that blog post we made use of the WebClient class to make our request to the service. In Windows Store applications we do not have access to this class and instead make use of the System.Net.Http.HttpClient class. We will call this common method GetResponse and use the following code:

In this last common method, we will clear the map and the results panels of our application. The map has to be cleared before we process any new requests. We will also need this functionality for the clear map button. We will call this method ClearMap and use the following code:

privatevoid ClearMap()

{

MyMap.Children.Clear();

routeLayer.Shapes.Clear();

//Clear the geocode results ItemSource

GeocodeResults.ItemsSource = null;

//Clear the route instructions

RouteResults.DataContext = null;

}

We can now add the button handler for the Clear Map button. All this button handler will do is call our ClearMap method. The handler will look like this:

privatevoid ClearMapBtn_Click(object sender, RoutedEventArgs e)

{

ClearMap();

}

Adding the Geocoding Logic

We can now add the geocoding logic to the MainPage.xaml.cs file. When the user presses the geocode button we will get their input and check that it is a valid string. If it is, we will then create the URL to geocode the query against the Bing Maps REST Location service and then pass this URL to our GetResponse method. Next, we will display all the locations results on the map as pushpins and add a tap event that will trigger the name of the location to be displayed to the user. We will also pass the results to the GeocodeResults ListBox which will list the location results in the side panel. The GeocodeBtn_Click event handler will look like this:

new Bing.Maps.Location(l.Point.Coordinates[0], l.Point.Coordinates[1]);

//Create a pushpin each location

Pushpin pin = newPushpin(){

Tag = l.Name,

Text = i.ToString()

};

i++;

//Add a tapped event that will display the name of the location

pin.Tapped += (s, a) =>

{

var p = s asPushpin;

ShowMessage(p.Tag asstring);

};

//Set the location of the pushpin

MapLayer.SetPosition(pin, location);

//Add the pushpin to the map

MyMap.Children.Add(pin);

//Add the coordinates of the location to a location collection

locations.Add(location);

}

//Set the map view based on the location collection

MyMap.SetView(newLocationRect(locations));

//Pass the results to the item source of the GeocodeResult ListBox

GeocodeResults.ItemsSource = r.ResourceSets[0].Resources;

}

else

{

ShowMessage(“No Results found.”);

}

}

else

{

ShowMessage(“Invalid Geocode Input.”);

}

}

Next, we will add a SelectionChanged event to the ListBox. When triggered, it will call a method named GeocodeResultSelected. This method will zoom in to the selected location. The following code will be used for this method:

new Bing.Maps.Location(item.Point.Coordinates[0], item.Point.Coordinates[1]);

//Zoom into the location

MyMap.SetView(location, 18);

}

}

If you run the application and do a search for “London” your application should look like this:

Adding the Routing Logic

We can now add the routing logic to the MainPage.xaml.cs file. When the user presses the “Get Directions” button we will first get their input locations and check that they are valid strings. If they are valid, we will create the URL to get the driving route between the two locations and pass this URL to our GetResponse method. Next, we will display the route line on the map along with a pushpin for the start and end points. We will also display the route itinerary items in a ListBox in the side panel. The RouteBtn_Click event handler will look like this:

At this point, we have accomplished all that we set out to do in this blog post. If you run the application now and calculate a route from “New York” to “Miami” you should end up with something like this:

Dear Sir,
I have problem while routing in bing maps.
I am sending you the link of the screenshot where i am having error: http://qfs.mobi/f347684
I am finding error in the following button click event: private async void GeocodeBtn_Click(object sender, RoutedEventArgs e).
Please do the needful as early as possible

I don’t have a sample at the moment but this is really easy. You can use the Bing Spatial Data Services to find nearby points of interest as described in this (JavaScript based) post: https://rbrundritt.wordpress.com/2012/01/17/dynamically-updating-data-in-bing-maps-v7/ To do this in C# you would use the same service but use WebClient to download the response. You can have XML returned by the service instead of JSON and then simple use a DataSet in C# to parse the XML as that’s what the response is, an XML version of a DataSet.