Implementing Location-Based Functionalities with the MapPoint Web Service
This article by Mohanraj Annamalai illustrates how to find the center point from a set of Latitudes and Longitudes. The article also discusses how to programmatically generate a map using the Microsoft MapPoint Web Service and implement interactive features like: re-centering a map based on the clicked location, panning north, south, east, and west.
When implementing location-based functionalities (e.g. rendering maps)
for business applications, finding the center of the data set is an
important step. The mode the number with the highest frequency - does a nice job in telling where the
center of the data set is. This article illustrates how to find the center point from
a set of Latitudes and Longitudes. The article also discusses how to programmatically generate maps using the Microsoft MapPoint Web
Service and implement interactive features like: re-centering a map based on the clicked location, panning north, south, east, and west.
The ASP.NET (VB.NET) source code for this article illustrates scenario of generating an area map in Los Angeles, CA with a set of (fictitious) drug store locations. The following are the requirements to run the source code:
.NET 2.0 frame work, and VS.NET 2005
SQL Server 2005 Express edition
MapPoint Web Service developer account to access the staging environment. Note: you can apply for developer account at this URL -
Download the code sample.
The SQL server database (in the App_data folder) has one table with following columns Name, Address, City, State, Zip, Latitude, and Longitude. In a real life scenario we know only the Address of the locations, but for the simplicity, I am assuming Latitude and Longitude are available in the database. The Microsoft MapPoint web service provides API functions to calculate Latitude and Longitude of any location based on its address. So in any real-life application when the location details are stored, an extra step of finding and storing Latitude and Longitude will help in the future.
Generating dynamic maps programmatically for business applications involves the following aspects:
Based on the users query (input parameters) find a set of relevant locations from the database.
Render a map using the resulting locations details.
Adding interactive features to the map.
To create dynamic maps, the MapPoint Web Service provides Find, Route, and Render services along with some utility services. We can generate maps using these services in many approaches like maps based on proximity search or by bounding rectangle etc. In the given source code maps are generated with render services of MapPoint using a center point. There are two ASP.NET pages in the given sample code. One page (default.aspx) has all the user interface controls like text box, command button, and image buttons. The other page (MapImg.aspx) has the logic of generating the dynamic map using Render services of MapPoint.
The Page_Load event of MapImg.aspx deals with three different cases. We will take a look at the case of generating map based on the given City and State. Query the database to get an ordered (by Latitude and Longitude) list of locations for the given City and State. Loop through the fetched DataTable to create array of locations and its pushpins to generate the map. The next step is to find the center point of the found locations. To do that, loop through all the locations again rounding them to two decimal places and find the mode of the data set. The found mode gives one set of Latitude and Longitude which is used as the center point of the map. Based on the given Scale and dimensions (height and width) and the Center point the map will include pushpins. Depending on the specific data you can increase or decrease the scale, dimensions and rounding precisions to generate maps with desired number of pushpins.
To add interactive features to the map, you have to render the map with the same locations but recalculating the Center Point. In the case of making the user-clicked position as the center point. Use the Image button web server controls event arguments (pixel co-ordinates). By passing the pixel co-ordinates to the ConvertToLatLong API of the Render services, the new center point is calculated and it is used to generate the map.
To pan north or south, increase or decrease previous center-points Latitude and keep Longitude same. Similarly to pan east or west, increase or decrease previous center-points Longitude and keep Latitude same.
Before running the provided source code, in the web.config file change the key values MPUser and MPPass to the valid developer account user-id and password. Run the source code for this article, in the screen enter Los Angeles and CA for City and State text boxes then click the Go button to generate a map as shown in the figure 1.
In the map when you click on the push pin Asia Drugs, a map with that location as center is generated as shown in figure 2.
Now when you click the East button (right arrow), the following map is generated: