In this article we will look at how to use ASP.Net to access the MapPoint Web Service and display the results on a Bing Maps map. This article focuses on accessing data from custom data sources using the FindNearBy, FindByProperty, FindByID, and FindNearRoute MapPoint Web Service methods. The programming model we follow is the classic model, view, control model, where a front end component concerns itself with the user interface (view), a back end component concerns itself with making data requests (model), and a middle component mediates the requests and responses between the two (control).

Note that the complete listing of the source files developed in this article is available in the Source Listings section.

Getting Started

In order to make our application work, we will need to develop the following components, as illustrated in Figure 1.

A front end (view) component that uses a Bing Maps AJAX Control 6.3 and sends an HTTP request to the middle component for MapPoint data, and displays that data.

A middle (control) component that receives the front end component's HTTP requests, translates them into the back end component's MapPoint Web Service requests, and returns that data to the front end component.

A back end (model) component that receives requests from the middle component, converts those into MapPoint Web Service requests, and returns that data to the middle component.

Figure 1. The integration data flow

Creating the ASP.Net Project

Open Visual Studio (2005 was used for this article, so some minor differences may occur if you use a different version), select New, Project, then ASP.Net Web Application. Name your project MWSFind.

Creating the Front End View Component

The front end component consists of some HTML and JavaScript methods that make HTTP requests to the middle component.

Adding the Initial HTML

If Solution Explorer is not visible, open it by clicking View, Solution Explorer. Right-click MWSFind, select Add, New Item. Select HTML Page and enter Default.htm. If Visual Studio does not open Default.htm in the editor, double-click Default.htm in Solution Explorer and replace its contents with that in Listing 1.

Creating a Virtual Directory to Test Your Code

Once you have saved Default.htm, one way to test it is by creating a Virtual Directory in Microsoft Internet Information Services (IIS) that points to your project. Open IIS (typically start, Administrative Tools, Internet Information Services), navigate to the Default Web Site, right-click the Default Web Site, and select New, Virtual Directory. Enter MWSFind as the alias, then the path to your MWSFind project to create a Virtual Directory that references MWSFind.

If you open http://localhost/MWSFind in a Web browser, you should see something like Figure 2.

Now we can make an HTTP call by initializing the xmlhttp object, and using its open and send methods. When the call is returned, it is automatically processed by the SearchHandler function.

Adding the AddPin Function

Our middle component is going to return a string representing a set of JavaScript calls. Although we could have it return a set of map.AddPushpin calls, that creates too much of a dependency between our front and back end. Instead, we are going to have our back end call the JavaScript function AddPin, as shown in Listing 5.

Adding the Button Handler Functions

You probably noticed that the four buttons we created in the BODY section refer to four missing functions, FindNearBy, FindByProperty, FindNearRoute, and FindByID. We will implement these four functions in this section.

Adding the FindNearBy Function

The FindNearBy function shown in Listing 6 uses the center of the map as the center of the search. It sends the name of the datasource, the entity type name, and the search radius to the middle component for processing.

Adding the FindByProperty Function

The FindByProperty function shown in Listing 7 uses the center of the map as the center of the search. It sends the data source name, the entity type name, an expression and a list of parameters to the middle component for processing.

Adding the FindByID Function

The FindByID function shown in Listing 8 uses the center of the map as the center of the search. It sends a comma delimitated string representing the array of entity IDs, the datasource name, and the entity type name to the middle component for processing.

Adding the FindNearRoute Function

The FindNearRoute function shown in Listing 9 uses the center of the map as the center of the search. It uses the new GetDirections method in Bing Maps to draw the route on the map. The route options are set so that it uses the MWS routing method. In our request to the handler we send the starting and ending coordinates along with MapPoint datasource name the route is being drawn in, the custom datasource the user is using, the entity type name, the route type and the distance from the route to search.

Creating the File and Adding the Initial C#

Once we have our basic handler, we need to modify the ProcessRequest method to meet our needs. We need to determine which find method is being called, check the parameters, create our MWSFindGenerator class, get our data, and write it back to the requesting page. Replace ProcessRequest with the code shown in Listing 10.

The FindByID function in the front end component sends a comma-delimited string to represent an array. Therefore, the middle component requires a function to convert this string into an integer array so that it can be sent to the MapPoint Web Service. This is done using the ParseIntValues method as shown in Listing 11.

Creating the Back End Model Component

The back end component is an ASPX page consisting of a C# method for each of the front end component's Find methods (FindNearBy, FindByProperty, FindByID, and FindNearRoute). These methods all return a string consisting of a semi-colon separated sequence of calls to AddPin. To make this work, we need to do the following.

Create a Web reference to MapPoint Web Service

Create an instance of the MapPoint Find proxy

Create an instance of the MapPoint Route proxy

Issue a call to the appropriate MapPointFind method

Parse the results

We already have an ASPX page, Default.aspx, but you probably notice ProcessRequest creates a MWSFindGenerator object. We could just change the class name in Default.aspx, but that is confusing, so we next rename Default.aspx as MWSFindGenerator.aspx. Right-click Default.aspx in Solution Explorer and rename it as MWSFindGenerator.aspx.

We start by declaring some internal constants for our user name and password (change these to your own values). We also need a member variable for our Find and Route Services.

Next, in the constructor, we create an instance of the MapPoint Web Service Find Service and Route Service. We also assign our username and password as credentials, and turn on pre-authentication to help speed up repeated requests to the service.

Adding the FindNearBy Method

This method is called with a latitude, a longitude, a custom datasource name, an entity type name, and a search radius. The FindNearBy method performs the following actions.

Creates a FindNearbySpecification object

Defines a datasource

Sets the latitude and longitude

Filters the results to only show results that match the entity type name

Adding the FindRoute Method

This method is called with a custom datasource name, an entity type name, the start and end coordinates of the route, the MapPoint datasource containing the route, the route type, and the distance around the route to search. The FindNearRoute method performs the following actions.

Creates a route object that is equivalent to the route in Bing Maps

Creates a FindNearRouteSpecification object

Defines a datasource

Filters the results to only show results that match the entity type name

Adding the BuildPin Method

This method processes each of the returned MapPoint result objects and generates a string calling the AddPin method. The most challenging part of this method is extracting the interesting properties out of the Location object. Since the MapPoint Location object's properties array is numerically indexed, we convert it to a Dictionary object so that we can extract values by name.

The Find Results

Once you have saved your work and rebuilt the project to ensure you have no errors, click each of the buttons in turn. You should see something like the results shown in Figure 4 for FindNearBy, Figure 5 for FindByProperty, Figure 6 for FindNearRoute and Figure 7 for FindByID.

Figure 4. The FindNearBy results

Figure 5. The FindByProperty results

Figure 6. The FindNearRoute results

Figure 7. The FindByID results

Conclusion

Once you understand the basic process in place for making AJAX calls, you can easily link to other datasources. You can also easily expand upon these methods to include other MapPoint methods.

This article was written by Richard Brundritt. Richard is a software development enginner at Infusion Development.

Source Listings

The sections contains a complete listing for the three source files in this article, Default.htm, MWSFindGenerator.aspx.cs, and MWSFindHandler.ashx.