Sample WPF application consuming Bing Maps Web Services

By Sarah Rice,2014-05-14 09:22
20 views 0
Here is a sample WPF application, that consumes GeocodeService, SearchService, ImageryService and RouteService that are part of Bing Maps web service

Here is a sample WPF application, that consumes GeocodeService, SearchService, ImageryService and

    RouteService that are part of Bing Maps web service

    ? Download - 449.17 KB Download BingMapsDemo - 449.17 KB


    This article demoes sample WPF application that consumes Bing Maps web service that comes handy if

    your business requirements indicate that you need to build a desktop application. This article

    demonstrates using the Geocode, Imagery, Route, and Search services of the Bing Maps Web Services in

    a desktop application, using Windows Presentation Foundation (WPF) and C#.


    Ever wish to have bing maps integrated with your windows application? Here is the solution. All you

    need is Visual Studio 2008, .Net Framework 3.0 and Bing Maps Platform Developer Account credentials.

    You can sign up for a free developer account at the Bing Maps Account Center. The Bing Maps Web

    Services require that you have a Bing maps key to make requests. You can create a key when you sign

    into your account at Bing Maps Account Center. For more information about creating a key, see

    Accessing the Bing Maps Web Services Using the code

    Once you have your Bing Maps Account and the key, you are ready to start creating proxy classes that

    reference the Bing Maps Web Services. To do this you need to set up service references which provide

    the geocoding, mapping and search features. These are added as Windows Communication Foundation

    (WCF) services and Visual Studio builds proxy files for the project.






    The code is simple. The first thing you have to do before calilng any method of these services is to set the

    key that you got from your bing maps account. Once done, you create instance of service that you want

    to use and try identify the method that you want to invoke. Generally the methods need object of some

    type to be passed as its param, so spend some time understanding the param and what properties need to

    be set.

    You will notice that the app have 6 buttons. Here is what each button does -

    - this button converts an postal address into geo code (longititues and latitudes). This feature

    is based on Geocode Service. The Geocode Service offers methods to geocode addresses and reverse

    geocode locations.

    Here is the code the explains how to get the geo code for the specified address. It is very simple. First,

    start by seeting the key that you got from your bing maps account as the credential for GeocodeRequest.

    Next, set the filters. Once done, create the instance of GeoServiceClient and fire its GeoCode method.


    private GeocodeResponse GeocodeAddressGeocodeResponse(string address)


     GeocodeRequest geocodeRequest = new GeocodeRequest()

     // Set the credentials using a valid Bing Maps key

     geocodeRequest.Credentials = new GeocodeService.Credentials();

     geocodeRequest.Credentials.ApplicationId = key;

     // Set the full address query

     geocodeRequest.Query = address;

     // Set the options to only return high confidence results

     ConfidenceFilter[] filters = new ConfidenceFilter[1];

     filters[0] = new ConfidenceFilter();

     filters[0].MinimumConfidence = GeocodeService.Confidence.High;

     // Add the filters to the options

     GeocodeOptions geocodeOptions = new GeocodeOptions();

     geocodeOptions.Filters = filters;

     geocodeRequest.Options = geocodeOptions;

     // Make the geocode request

     GeocodeServiceClient geocodeService = new GeocodeServiceClient();

     GeocodeResponse geocodeResponse = geocodeService.Geocode(geocodeRequest);

     return geocodeResponse;


Here is a scren shot -

     - as the name suggests, you can convert the geo code into readable postal address. It

    is quite similler to Get GeoCode method explained above.

     - This method takes "what" and "where" part of the query. "What" is things like gas stations,

    barber, etc. and "where" is the location. It returns list of search results. The app shows the name and

    distance, but you could do more with the data received.

    Here is the code snippet that gets the search results near the location specified. private string SearchKeywordLocation(string keywordLocation)


String results = "";

    SearchRequest searchRequest = new SearchRequest();

// Set the credentials using a valid Bing Maps key

    searchRequest.Credentials = new SearchService.Credentials();

    searchRequest.Credentials.ApplicationId = key;

//Create the search query

    StructuredSearchQuery ssQuery = new StructuredSearchQuery(); string[] parts = keywordLocation.Split(';');

    ssQuery.Keyword = parts[0];

    ssQuery.Location = parts[1];

    searchRequest.StructuredQuery = ssQuery;

//Define options on the search

    searchRequest.SearchOptions = new SearchOptions(); searchRequest.SearchOptions.Filters =

     new FilterExpression()


     PropertyId = 3,

     CompareOperator = CompareOperator.GreaterThanOrEquals,

     FilterValue = 8.16


//Make the search request

    SearchServiceClient searchService = new SearchServiceClient(); SearchResponse searchResponse = searchService.Search(searchRequest);

//Parse and format results

    if (searchResponse.ResultSets[0].Results.Length > 0) {

     StringBuilder resultList = new StringBuilder("");

     for (int i = 0; i < searchResponse.ResultSets[0].Results.Length; i++)


     resultList.Append(String.Format("{0}. {1}, [Dist]{2}, [Loc]{3}, [Id]{4}\n",

    i + 1,






     results = resultList.ToString();



     results = "No results found";

return results;


Here is the snap shot -

     - This method receivs geo code and shows the map of that area. See #5 point below for

    more details

     - This method receivs the english like address and shows the map. You will also see a scroll bar

    that you could use to set the zoom level.

    Here is the code snippet. It is quite simple. You create instance of Imagery Service and fire its GetMapUri method. This method will take object of MapUriRequests. This is the object where you get the longitude and latititude and the zoom level.

    private string GetImagery(string locationString, int zoom)


     MapUriRequest mapUriRequest = new MapUriRequest();

     // Set credentials using a valid Bing Maps key

     mapUriRequest.Credentials = new ImageryService.Credentials();

     mapUriRequest.Credentials.ApplicationId = key;

     // Set the location of the requested image

     mapUriRequest.Center = new ImageryService.Location();

     string[] digits = locationString.Split(',');

     mapUriRequest.Center.Latitude = double.Parse(digits[0].Trim());

     mapUriRequest.Center.Longitude = double.Parse(digits[1].Trim());

     // Set the map style and zoom level

     MapUriOptions mapUriOptions = new MapUriOptions();

     mapUriOptions.Style = MapStyle.AerialWithLabels;

     mapUriOptions.ZoomLevel = zoom;

     // Set the size of the requested image in pixels

     mapUriOptions.ImageSize = new ImageryService.SizeOfint();

     mapUriOptions.ImageSize.Height = 200;

     mapUriOptions.ImageSize.Width = 300;

     mapUriRequest.Options = mapUriOptions;

     //Make the request and return the URI

     ImageryServiceClient imageryService = new ImageryServiceClient();

     MapUriResponse mapUriResponse = imageryService.GetMapUri(mapUriRequest);

     return mapUriResponse.Uri;


Here is the screen shot -

     - This method takes two addresses and retuns back the route between these two end points. The

    Route Service can calculate a route between waypoints. As with the other services, you need to start by creating a request object. A object includes the array of waypoints to route between and

    options on how to calculate the route. The result object contains a set of step by step instructions.

    In order to get the route, you will need to use RouteService. This service needs an instance of WayPoint where you set the co ordinates of start and end address. Once you are done populating the WayPoint object, just call RouteService.CalculateRoute method. Here is the code snippet -

    //Parse user data to create array of waypoints

    string[] points = new string[]{start, end};

    Waypoint[] waypoints = new Waypoint[points.Length];

    int pointIndex = -1;

foreach (string point in points)



     waypoints[pointIndex] = new Waypoint();

     string[] digits = point.Split(','); waypoints[pointIndex].Location = new RouteService.Location();

     waypoints[pointIndex].Location.Latitude = double.Parse(digits[0].Trim());

     waypoints[pointIndex].Location.Longitude =


     if (pointIndex == 0)

     waypoints[pointIndex].Description = "Start";

     else if (pointIndex == points.Length)

     waypoints[pointIndex].Description = "End"


     waypoints[pointIndex].Description = string.Format("Stop #{0}", pointIndex);


    routeRequest.Waypoints = waypoints;

// Make the calculate route request

    RouteServiceClient routeService = new RouteServiceClient(); RouteResponse routeResponse = routeService.CalculateRoute(routeRequest);

Here is the snap shot -

Points of Interest

    Now you know how to consume Bing Maps web Geocode, Imagery, Route, and Search services. Feel free to play with the code and dig deeper. Have fun!


    Initial version.


    This article, along with any associated source code and files, is licensed under The Code Project Open

    License (CPOL) About the Author

     Raj Dhrolia

    Location: United States


Report this document

For any questions or suggestions please email