Developing a Mobile Application Using Bing Maps Web Services

The new set of Bing Maps Web Services gives the user the ability to access the many tools of Bing Maps from alternate applications such as desktops, servers and mobile devices. Additionally, the Bing Maps Web Services provide a simple mechanism for mobile applications to geocode locations, reverse geocode, get maps and generate routes. This makes integration with Windows Mobile devices much easier. This article describes how to build and test a Windows Mobile enabled web application that leverages Bing Maps Web Services.

Overview

The constraints of mobile device applications are very different than the traditional web or desktop applications. Unlike a desktop or web application, the screen real estate is very compact and the controls are limited in functionality. These constraints force you to rethink your user experience. Simply building a form with a bunch of buttons isn’t very useful on most mobile devices. In addition, the occasionally connected nature of the device is problematic for any application that relies on web connectivity. Because you can’t guarantee whether the device will have or maintain a connection, the standard practice is to minimize the amount of network traffic expected. Similarly, network connections to mobile devices (even 3G) are slower and more bandwidth constrained than normal broadband. This puts an even larger emphasis on reducing network traffic.

Given these constraints, the first question about architecture involves how the application will be deployed and used. If you are building a consumer application for individual users, you want the entire application contained on the mobile device. However, if you are building for an enterprise, you might consider a three tier application where you can control the security of your credentials and your bandwidth on a server, and minimize the footprint of the mobile application. If you take the three tier approach, you also have to consider what the middle tier will be. You can use some sort of service (for example, build a WCF layer that encapsulates Bing Maps) or build a mobile enabled web application. The choice of architecture depends heavily on how you intend to use the application and the constraints of your target devices.

Self Contained Application

The first architecture approach consists of accessing the Bing Maps Web Services directly from a Windows Mobile application.

Dd483215.27ba7e61-4302-41a5-b9a0-08050acdd7ef(en-us,MSDN.10).png

This is the least complicated solution, however there are many things to consider before going this route. Accessing the Bing Maps Web Services directly requires the mobile device to process all the data that is returned from the web services, which in most cases results in slow performance. Another major consideration is that mobile networks are typically more expensive and slower than a standard internet connection. By accessing the Bing Maps Web Services directly, all responses to service requests contain the full service response information. In most cases this is more information than you require. One final shortcoming is that by accessing the Bing Maps Web Services directly, your Bing Maps account credentials would be embedded into the mobile application that you would be distributing.

Multi-tier Application

The multi-tier solution consists of having a mobile device connect to a web service which connects to the Bing Maps Web Services and processes the requests.

Dd483215.a48aaa64-7e1d-427a-8ba5-f155c725cfd4(en-us,MSDN.10).png

This method is a bit more complicated and requires you to have a server to host your web service. However, there are several benefits of this method. By creating a web service to access the Bing Maps Web Services, you can process the returned results from Bing Maps and only pass the minimal information required to the mobile device. This significantly cuts down on the total amount of data that is transferred to the mobile device, which reduces the overall end user cost of using your application, increasing the performance. Similarly, you can create a temporary cache which can easily handle the case of the mobile device losing connection or having a weak signal.

By creating a web service your Bing Maps credentials reside on your hosted server and not in the client’s application, thus creating a greater level of security. However, you do add complexity in that you have to develop, package, test and deploy a mobile application.

Multi-tier Web Application

The third architecture follows the same principles and gains many of the advantages of building a hosted Web service. However, building a mobile web application offers several other advantages, specifically:

  • You can test the application without using a mobile device or emulator.

  • You do not have to deploy anything to the mobile devices.

  • Almost all mobile devices have browser capability.

  • ASP.NET will automatically render the appropriate markup language for the device.

With the advances in mobile devices and the .NET Framework, you don’t even have to build a special mobile web application. As long as your web application uses standard components and avoids JavaScript, most mobile browsers will render it correctly.

The Application

This article focuses on implementing the third solution as it offers more benefits and is easier to implement than either of the other solutions. However, most of the code is easily convertible into a self-contained application or a hosted set of services if you choose to follow either of those architectures instead.

Because the sample only requires a browser on a mobile device, the code is similar to other Bing Maps Web Services samples outlined in the following articles:

Rather than demonstrate every Bing Maps Web Services method, the application developed in this article focuses on solving the most common mobile device user scenario: providing a simple mechanism to retrieve a map showing the user’s current location, or the location of any address.

Setup

The examples in this article require a single Web Application project. However, you will also want to set up an emulator to test the application. The emulator portion is covered in the second half of this article.

Prerequisites

In order to run the code in this article, you need Bing Maps for Enterprise Developer Account credentials. You can sign up for a free developer account on the Bing Maps Developer Account Request site. Once you have credentials, you will be able to access the services using a token, as explained in the article Implementing Customer Identification.

In addition, the examples require that Visual Studios 2008 Professional or above is installed. If you wish to use Visual Studio 2005, you will have to install and configure a device emulator and Microsoft ActiveSync.

The code in this article was created on a Windows Vista machine, using Visual Studio 2008 Professional. There may be slight variations for other operating systems and development environments.

Creating the Website Project

To get started:

  1. Open Visual Studio 2008.

  2. From the main menu select File, New, and then select Web Site.

  3. In the New Web Site dialog, select the ASP.NET Web Site template.

  4. Change the name of the project to VEMobile as shown below.

    Dd483215.002d69f9-e050-437f-bf60-72e55091b024(en-us,MSDN.10).png
  5. Click OK to create the Web site project.

Your project is created with a single Default.aspx page. You will use this page to allow the user to submit an address. You will eventually create a second page for displaying the results.

Creating the Web Application

The web application encapsulates all of the calls to the Bing Maps Web Services. Each exposed web page uses a compact layout based on the most commonly used mobile friendly controls. Using this approach, you can tune the server side component to cache and filter any of the information necessary to make the web application as efficient as possible for mobile devices.

Setting up Client Tokens

In order to access the Bing Maps Web Services, you need to include a client token which identifies you. Add the functionality to request a client token to the Default.aspx.cs page and then store the token in the session state.

Reference the Common Service

The first step is to reference the Bing Maps Token Service:

  1. In the Visual Studio Solution Explorer window, right click on the VEMobile project and select Add Web Reference.

  2. Set the Url to https://staging.common.virtualearth.net/find-30/common.asmx and press the Go button.

  3. Enter your Bing Maps Developer Account credentials into the security window that appears.

  4. Change the web reference name to VEStagingToken as shown below.

    Dd483215.937a88ea-1ac7-4acd-ae03-070ba2635d68(en-us,MSDN.10).png
  5. Press Add Reference.

  6. Enter your Bing Maps account credentials into the security window that appears.

Request the Token

In order to acquire a token, you need to prepare a TokenSpecification which contains:

  • Your Bing Maps Developer Account credentials – These consist of the 4 to 6 digit user ID and the password of your Bing Maps developer account.

  • Client IP address – This field is used for tracking purposes. Ideally you want to use the IP address of the user that is running your web application.

  • The expiration length in minutes – Each token has a built in expiration time ranging from 15 minutes to 8 hours.

To create a method to request a token:

  1. Open Default.aspx.cs.

  2. Add a reference to the VEStagingToken web service by putting the following line at the top of the file:

    using VEStagingToken;
  3. Now retrieve the IP address of the user of the web application. Using sample code from the Implementing Customer Identification article, add the following code:

    private string GetClientToken()
    {   VEStagingToken.CommonService commonService = new VEStagingToken.CommonService();
        commonService.Url = "https://staging.common.virtualearth.net/find-30/common.asmx";
        commonService.Credentials = 
           new System.Net.NetworkCredential("VE Account id", "VE password");
    
        // Create the TokenSpecification object to pass to GetClientToken.
        TokenSpecification tokenSpec = new TokenSpecification();
    
        //Use the currently exposed public IP for the page.
        tokenSpec.ClientIPAddress = Page.Request.UserHostAddress;
    
        // The maximum allowable token duration is 480 minutes (8 hours).
        // The minimum allowable duration is 15 minutes.
        tokenSpec.TokenValidityDurationMinutes = 480;
    
        // Now get a token from the Bing Maps Token Service.
        return commonService.GetClientToken(tokenSpec);
    }

You now have the ability to generate a client token. For simplicity sake this article shows the credentials embedded directly in the Default.aspx.cs file in clear text. For security reasons, you should move these string values to a protected data store. For example, you can put the values in your configuration file using protected configuration, as described here: http://msdn.microsoft.com/en-us/library/53tyfkaw.aspx.

The next task is to create the client token and attach it to the session. Add the following code to the Page_Load() method of Default.aspx.cs:

Session.Add("VEToken", GetClientToken());

Referencing the Bing Maps Web Services

Although you have successfully called Bing Maps Web Services to retrieve a client token, you need to add service references to provide useful geocoding, mapping and search features. Note that you have to add these references using Add Service Reference rather than Add Web Reference (as you did when referencing the Token Service). By adding WCF services, Visual Studio builds slightly different proxy files.

You have to add four services. For each service:

  1. Right click on the References folder in the VEMobile project and select Add Service Reference. If you have selected the VEMobile project in the solution explorer, you can also select Project, then Add Service Reference from the main menu.

  2. Copy the service URL from the following table into the Address text box and then press the Go button.

    NamespaceAddress

    VEGeocodeService

    http://staging.dev.virtualearth.net/webservices/v1/geocodeservice/geocodeservice.svc

    VESearchService

    http://staging.dev.virtualearth.net/webservices/v1/searchservice/searchservice.svc

    VEImageryService

    http://staging.dev.virtualearth.net/webservices/v1/imageryservice/imageryservice.svc

    VERoutingService

    http://staging.dev.virtualearth.net/webservices/v1/routeservice/routeservice.svc

  3. Enter the corresponding service name in the Namespace field and then press the OK button.

  4. Repeat steps 1-3 for each of the services listed in the table.

  5. You now have to reference these services in the various web pages you create.

  6. Add the following code to the top of the Default.aspx.cs file:

    using VEGeocodingService;
    using VESearchService;
    using VEImageryService;
    using VERoutingService;

The default response size for these services is 65536 bytes. This may not be enough large enough for search service results. If the message size is too small for the data, you will see error messages rather than receiving results. If you wish to increase the response message size:

  1. Open web.config.

  2. Look for a <binding> tag where the name attribute is BasicHttpBinding_ISearchService.

  3. Change the value of the maxBufferSize and maxRecievedMessageSize attributes to 500000.

  4. Repeat steps 2 and 3 for the <binding> tag with the name attribute of BasicHttpBinding_IInternalSearchService.

Internal Methods

To make your code more efficient, create some methods that are commonly used in your web project. The first method geocodes an address and the second method retrieves a map URI. These methods will be added to the Default.aspx.cs file.

Geocode an Address

The method that geocodes an address takes a client token and an address string, and returns a Location object. If no results are found, is returned. You will also add a confidence filter so that only results with a high confidence are returned.

To create the GeocodeAddress method:

  1. Open Default.aspx.cs.

  2. Copy the following method into the file:

    private VEGeocodingService.Location GeocodeAddress(string address)
    {
        GeocodeRequest geocodeRequest = new GeocodeRequest();
    
        // Set the credentials using a valid Bing Maps token
        geocodeRequest.Credentials = new VEGeocodingService.Credentials();
        geocodeRequest.Credentials.Token = (String)Session["VEToken"];
    
        // 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 = VEGeocodingService.Confidence.High;
    
        GeocodeOptions geocodeOptions = new GeocodeOptions();
        geocodeOptions.Filters = filters;
    
        geocodeRequest.Options = geocodeOptions;
    
        // Make the geocode request
        GeocodeServiceClient geocodeService = new GeocodeServiceClient();
    
        GeocodeResponse geocodeResponse = geocodeService.Geocode(geocodeRequest);
    
        if (geocodeResponse.Results.Length > 0)
            if (geocodeResponse.Results[0].Locations.Length > 0)
                return geocodeResponse.Results[0].Locations[0];
    
        return null;
    }

Return a Map

The second method returns a map URI. This method takes a client token, latitude and longitude coordinates, a zoom level, and the width and height of the image to be returned. This method also places a pushpin on the latitude, longitude coordinate that is specified. And to make this method more dynamic, it takes a map style in the form of a string, which specifies the type of map to return. The map styles that can be used are “Road”, “Aerial”, and “Hybrid”.

To create the GetMapUri method:

  1. Open VEService.svc.cs.

  2. Copy the following method into the file:

    private string GetMapUri(double latitude, double longitude, int zoom, string mapStyle, int width, int height)
    {
        VEImageryService.Pushpin[] pins = new VEImageryService.Pushpin[1];
        VEImageryService.Pushpin pushpin = new VEImageryService.Pushpin();
        pushpin.Location = new VEImageryService.Location();
        pushpin.Location.Latitude = latitude;
        pushpin.Location.Longitude = longitude;
        pushpin.IconStyle = "2";
    
        pins[0] = pushpin;
    
        MapUriRequest mapUriRequest = new MapUriRequest();
    
        // Set credentials using a valid Bing Maps Token
        mapUriRequest.Credentials = new VEImageryService.Credentials();
        mapUriRequest.Credentials.Token = (String)Session["VEToken"];
    
        // Set the location of the requested image
        mapUriRequest.Pushpins = pins;
    
        // Set the map style and zoom level
        MapUriOptions mapUriOptions = new MapUriOptions();
    
        switch (mapStyle.ToUpper())
        {
            case "HYBRID":
                mapUriOptions.Style = VEImageryService.MapStyle.AerialWithLabels;
                break;
            case "ROAD":
                mapUriOptions.Style = VEImageryService.MapStyle.Road;
                break;
            case "AERIAL":
                mapUriOptions.Style = VEImageryService.MapStyle.Aerial;
                break;
            default:
                mapUriOptions.Style = VEImageryService.MapStyle.Road;
                break;
        }
    
        mapUriOptions.ZoomLevel = zoom;
    
        // Set the size of the requested image to match the size of the image control
        mapUriOptions.ImageSize = new VEImageryService.SizeOfint();
        mapUriOptions.ImageSize.Height = height;
        mapUriOptions.ImageSize.Width = width;
    
        mapUriRequest.Options = mapUriOptions;
    
        ImageryServiceClient imageryService = new ImageryServiceClient();
        MapUriResponse mapUriResponse = imageryService.GetMapUri(mapUriRequest);
    
        return mapUriResponse.Uri;
    }

Public Web Methods

The web application consists of two primary pages. The first page (default.aspx) allows the user to enter a street address and request a map. The second page (result.aspx) shows the resulting map with a pushpin at the requested location.

Creating the Method

In order for your default page to be able to return a map URI of an address you need to do the following:

  • Generate a client token.

  • Geocode the address.

  • Request a map URI for the geocoded location.

Since this method generates a map, you need to get a zoom level, a map style, and the dimensions of the map image that will be returned.

To create the MapAddress method:

  1. Open Default.aspx.cs.

  2. Copy the following method into the file:

    private string MapAddress(string address, int zoom, string mapStyle, int width, int height)
    {
        string clientToken = GetClientToken();
    
        VEGeocodingService.Location latlong = GeocodeAddress(clientToken, address);
        double latitude = latlong.Latitude;
        double longitude = latlong.Longitude;
    
        return GetMapUri(clientToken, latitude, longitude, zoom, mapStyle, width, height);
    }

Creating the Form

The next step is to create the form which allows the user to enter the address. This example assumes a screen resolution of 240x320, as it is one of the most popular resolutions. Therefore, all controls and result information must fit in a portrait area no more than 240 pixels wide and 320 pixels high.

  1. Switch to the Design view of Default.aspx.

  2. Add a Label.

  3. Change the Text property of the label to “Enter Address:”.

  4. Add a TextBox below the label.

  5. Change the text box ID property to “TextAddress”.

  6. Change the Width of the text box to 240 pixels.

  7. Add a LinkButton below the text box.

  8. Change the Name property of the link button to "LinkSearch".

  9. Change the Text property of the link button to “Find Location”.

When you have created the form, it should look similar to this:

Dd483215.658b86b1-52cc-480b-a9eb-dcd93f9e722d(en-us,MSDN.10).png

Wiring the Controls

The last step is to wire up the link button to execute the methods required to generate the image.

  1. Double click on the link button to open up the LinkSearch_Click method.

  2. Add the following code to the method:

protected void LinkSearch_Click(object sender, EventArgs e)
{
  String result = MapAddress(TextAddress.Text, 17, "HYBRID", 240, 320);
  Response.Redirect(result);
}

Note that the map zoom (level 17), style and size are all specified in this method. If you wish to change any of these parameters, you can add more controls to the form or set different default settings here.

Testing the Application

You can test the application in the web host built into Visual Studio:

  1. Save your project.

  2. Press F5 to start the application. You should see the simple form:

    Dd483215.071fb607-9c91-4ef6-982a-26c62dbd788e(en-us,MSDN.10).png
  3. Enter 1 Microsoft Way, Redmond, WA into the address field and click the Find Location link. The results should appear similar to the following:

    Dd483215.6a1fec46-31ba-40f3-a7b1-3ea544807f4e(en-us,MSDN.10).png

Configuring and Deploying the Website

Although you have built and tested the website on a local server, you should also test it on an emulator. However, the local host inside Visual Studio does not allow connections from outside devices. Therefore, you have to deploy your website to an IIS server in order to connect from a mobile device or emulator.

To deploy your application:

  1. Right click on the solution in the solution explorer and select Publish Web Site.

  2. In the Publish Web Site wizard, choose a path for your final compiled site.

    Dd483215.53582a02-3a51-4efe-a1a9-09d091f3d928(en-us,MSDN.10).png
  3. Click OK.

  4. Start IIS Manager.

  5. Add a New Virtual Directory with the Alias set to VEMobile and the path pointing to your export path from Visual Studio (c:\VEMobile).

    Dd483215.77d1b69a-6e54-48ce-b2ae-72808d44dd05(en-us,MSDN.10).png
  6. Press OK.

  7. Right click on your new Virtual Directory and select Convert to Application.

    Dd483215.5d5f11ce-d54a-45f3-921f-a2ee93617109(en-us,MSDN.10).png
  8. Press OK.

  9. Select the Authentication link in the IIS section.

    Dd483215.559fabc5-5d72-473b-8bcf-9565f4a085a3(en-us,MSDN.10).png
  10. Verify that Anonymous Authentication is enabled.

    Dd483215.94945436-6851-4784-ab4c-2526e6295fd4(en-us,MSDN.10).png
  11. Test the application by opening up an Internet Explorer browser and navigating to http://localhost/vemobile/default.aspx. You should see the image in Figure 6.

Testing the Application on an Emulator

Once your website is properly hosted, the next task is to set up your emulator and connect to the site.

Connect and Dock your Emulator

To set up your emulator:

  1. Select Tools on the main menu of Visual Studio.

  2. Select Device Emulator Manager.

  3. Scroll down until you see the emulator you are using (Windows Mobile 6 Standard Emulator).

  4. Right click on your emulator.

  5. Select Connect. This opens an emulator window.

    Dd483215.c2ed8497-fbb6-46aa-9c26-8e18bbf176aa(en-us,MSDN.10).png
  6. In the Device Emulator Manager, right click on your emulator.

  7. Select Cradle.

    Dd483215.47ef5d25-8620-4f21-a3f7-682dd3979c8b(en-us,MSDN.10).png

Visual Studio is now able to sync with the Windows mobile device emulator.

Configuring Emulator Connections

In order for your emulator to connect to the Web, you have to use Windows Mobile Device Center (or ActiveSync if you are on Windows XP) to configure the connection mode.

  1. Start the Windows Mobile Device Center from your control panel.

  2. If your emulator is connected and cradled, you should see options for setting up your device:

    Dd483215.35d7bde2-a878-4791-a451-e07401dde0e5(en-us,MSDN.10).png
  3. Click Connect without setting up your device.

  4. Click on Mobile Device Settings and then Connection Settings.

  5. Make sure the following settings are configured:

    • Allow USB connections should be checked

    • Allow connections to one of the following: should be checked and set to DMA.

    • Allow data connections on device when connected to PC should be checked.

    Dd483215.c0682dd7-056d-4054-a682-9343c9bee800(en-us,MSDN.10).png
  6. Click OK and then close Windows Mobile Device Center.

  7. Test the device by using the built in Internet Explorer application to connect to a public site (for example, Windows Live).

    Dd483215.16c92797-b16a-43c3-b7d3-9ebb732fff2f(en-us,MSDN.10).png

Testing the Application

Test your application in your mobile device emulator by opening up the browser and pointing to your local installation of the VEMobile web application.

  1. From the home menu, select Start and then Internet Explorer.

  2. Click on the Menu button and then select Address Bar.

  3. Type in http://<your computer IP address>/vemobile. Unfortunately, you can’t simply point to localhost as that resolves to your mobile emulator rather than your local machine.

    Dd483215.b7b01735-9ddd-448c-8970-4a0c674badb2(en-us,MSDN.10).png
  4. Press Go.

  5. After the page loads, type a location into the address field of your mobile web page.

    Dd483215.d495fe41-125d-4c58-8273-0ff7ecc8a91a(en-us,MSDN.10).png
  6. Click on the Find Location link.

  7. You may see a security alert asking if you want to allow a post to the web. Click Yes.

  8. Your results should appear similar to the following figure:

    Dd483215.eb5aa7f2-0f5c-4e3c-9a54-c1673931bbcf(en-us,MSDN.10).png
Conclusion

The combination of Bing Maps with Windows Mobile offers the ability to not only search for points of interest and get directions between points but also the ability to display high quality maps. If you were to expand upon this code in the article you could easily reference your own geospatial data and give the user the ability to search for, map, and get directions to your locations.

This article was written by Richard Brundritt and Robert McGovern. Both Richard and Rob work for Infusion Development.

See Also

Other Resources

Bing Maps Web Services SDK

Page view tracker