By Chandu Thota
Price: $49.95 USD
£35.50 GBP
Cover | Table of Contents
MapPoint.AxMappointControl class instead of creating an ApplicationClass object to access the active map instance.
private AxMapPoint.AxMappointControl axMappointControl1;
AxMappointControl, you need to initialize the control by creating new map coverage by calling the NewMap method on the AxMappointControl class. This method takes the map region of type GeoMapRegionQuit method on the MapPoint.Application object:
//Define MapPoint Application instance
MapPoint.Application app = null;
//Obtain app references either via MapPoint.ApplicationClass or
//MapPoint.AxMapPointControl.ActiveMap.Application
. . .
//Clean up the MapPoint Application before you exit
if(app != null)
{
app.Quit( );
app = null;
}Quit method discards the current map and unloads all other items, such as add-ins, before exiting the application. However, calling this method asks the user whether she wants to save the map before discarding it. If you don't want users to have this choice, you can set the Saved property
to True on the application's active Map object:
app.ActiveMap.Saved = true;Map class's GetLocation method, you can easily find a location that corresponds to a given latitude and longitude measurement. This is also called geocoding in cartography terminology. This method takes latitude and longitude as System.Double values and returns a Location instance that represents the input latitude and longitude. This method also takes the altitude as an argument, but it is mainly used at the time when the location is displayed on a map, so you can freely pass one mile for the time being. The following code shows how to find a location using latitude and longitude:
//Get the reference to the active map instance
MapPoint.Map map = app.ActiveMap;
//Call the GetLocation method
to find location
//using the latitude and longitude
MapPoint.Location location =
m.GetLocation(41.33896, -122.43433, 1);GetLocation method because this method doesn't return the address all the time. Don't be disappointed, as there is still a way to find the nearest address using the current location. The idea is to basically do a hit-detection
around the found location to see if there are any addresses available. Before we get into the details of how to find out a location's address, let's look at the hit-detection in MapPoint 2004 in detail.Map object in MapPoint has the method, ObjectsFromPoint, which allows you to perform a hit-detection around any given point (x and y coordinates) on the screen. You can already get a point from any given location, so what does this method return? As the method name suggests, it returns an array of objects wrapped in a FindResults instance. The type of objects returned by this method depends on the current map altitude. For example, if you call this method at lower altitudes, it returns locations with street addresses; if you go to higher altitudes and call this method, it returns larger geographic areas, such as Zip Code, county, and time zone of the point. The following snippet shows how to call the MapPoint.Route object. You can access a Route object via the ActiveRoute property of a Map object (the Map object can be obtained either via the MapPoint.ApplicationClass object or MapPoint.axMapPointControl object using the ActiveMap property):
MapPoint.Route route = axMappointControl1.ActiveMap.ActiveRoute;MapPoint.Waypoint class. A valid route always contains two or more waypoints. Waypoints in a route are represented in the Route.Waypoints collection, so you use the Waypoints collection to add new waypoints:
//Get ahold of route object
MapPoint.Route route = axMappointControl1.ActiveMap.ActiveRoute;
//Add the location to the ActiveRoute
route.Waypoints.Add(loc, loc.Name);
//Obtain a waypoint from a given route
object index = 1;
MapPoint.Waypoint waypoint = route.Waypoints.get_Item(ref index);MapPoint.Waypoint object in many ways to specify a new route or modify an existing route. To change the location represented by a Waypoint object, use the Waypoint.Anchor property. This property is of type object because it can be either a Location or a Pushpin. If you are assigning a new location to a waypoint, you can do it like this:
//Obtain a waypoint from a given route
object index = 1;
MapPoint.Waypoint waypoint = route.Waypoints.get_Item(ref index);
//Assign a new location
waypoint.Anchor = newlocation;Quit method on the MapPoint.Application object when you are done with your tasks.DataSet class. An active Map object exposes the DataSets collection, which you use to access a valid DataSet object. How does a DataSets collection get its data wrapped as a DataSet object? The DataSets collection is different from traditional .NET collections—it not only exposes a collection of DataSet objects, but it also offers methods to import external data and to access MapPoint 2004 demographic data.DataSet object is similar to a data table with rows and columns and regular querying capabilities. However, a DataSet object allows you to query the records or data rows based on location information. Each query results in a Recordset object containing the records that satisfy the location-based query. The DataSet object can also be used to display data on maps. In essence, if you are using any data features (such as data maps, territories, etc.) in MapPoint 2004 APIs, the DataSet class is the root for all these tasks; Figure 3-1 shows the relationships between an active Map object, a DataSets collection, a DataSet object, and a Recordset object.
DataSet class. An active Map object exposes the DataSets collection, which you use to access a valid DataSet object. How does a DataSets collection get its data wrapped as a DataSet object? The DataSets collection is different from traditional .NET collections—it not only exposes a collection of DataSet objects, but it also offers methods to import external data and to access MapPoint 2004 demographic data.DataSet object is similar to a data table with rows and columns and regular querying capabilities. However, a DataSet object allows you to query the records or data rows based on location information. Each query results in a Recordset object containing the records that satisfy the location-based query. The DataSet object can also be used to display data on maps. In essence, if you are using any data features (such as data maps, territories, etc.) in MapPoint 2004 APIs, the DataSet class is the root for all these tasks; Figure 3-1 shows the relationships between an active Map object, a DataSets collection, a DataSet object, and a Recordset object.
DataSet and RecordSet objects shown in the MapPoint object model are different from the Dataset and RecordSet classes defined in the System.Data namespace in the .NET Framework.DataSet and RecordSet objects.|
Method Name
|
Description
|
|---|---|
DisplayDataMap
|
DataMap class. A DataMap object can only be created using a DataSet object by calling the DisplayDataMap method. The resulting map displays data on the current active map of the MapPoint ActiveX Control or the ApplicationClass object and returns a DataMap instance. The DataMap object exposes a set of read-only properties that define the data map's behavior. The only property for which you can set a value is the LegendTitle property, used in the legend pane.DataSet.DisplayDataMap method.Map object exposes a DataSets collection. You can access the demographic data by calling the GetDemographics method on the DataSets collection:
//Get the demographics dataset by calling the
//GetDemographics method
MapPoint.DataSet dataset
= map.DataSets.GetDemographics(MapPoint.GeoCountry.geoCountryUnitedStates);
GetDemographics method takes the country as an input argument and returns a DataSet object containing demographic data for the specified country.DataSet for other countries using MapPoint.GeoCountry enumeration. For example, to get demographics for the United Kingdom, call GetDemographics with the MapPoint.GeoCountry.geoCountryUnitedKingdom value.DataSet, loop through its field collection:
//Get dataset details
if(dataset != null)
{
//Get all field names
for(int i=1;i<=dataset.Fields.Count;i++)
{
object index = i;
//Add the field name in a listbox
listBox1.Items.Add(dataset.Fields.get_Item(ref index).Name);
}
}
DataSets.ImportData method to import external data. This method returns reference to a valid DataSet object upon successfully importing data
. The returned DataSet object is also added to the current active map's DataSets collection so that you can use the dataset later; however, once the map is closed, the imported dataset is lost.DisplayDataMap method, the ImportData method is also exposed on the DataSets collection, mainly due to the fact that the imported data will be stored as a data set in MapPoint and made available from the DataSets collection for later use. The signature for the ImportData method is as follows:
DataSets.ImportData(DataSourceMoniker, ArrayOfFields, Country, Delimiter, ImportFlags)DataSourceMoniker argument. Even though this argument is an DataSet object gives you the ability to query the dataset to access the records. These queries can be pure data access queries (using the DataSet.QueryAllRecords method) or geometry- and location-based queries (using the DataSet.QueryCircle, DataSet.QueryPolygon, and so on). In any case, a successful query returns a RecordSet object that you can loop through the records and access the fields and the values contained in that record. With this introduction, let's now look at different ways to query a dataset.DataSet using specific location queries for which the DataSet object provides methods, such as QueryCircle, QueryPolygon, and QueryShape. I will discuss the QueryCircle and QueryPolygon methods in this section and the QueryShape method shortly after introducing the shape concepts in the next section.QueryCircle method allows you to limit your query based on geographic distance. An example of this type of query is, "Find all orders that are being shipped to locations more than 100 miles away from my warehouse." In this query, you would use the QueryCircle method, specify the center of the circle as your warehouse, and set the radius of the circle to 100 miles.
//Create a new dataset
MapPoint.DataSet dataset = map.DataSets.AddPushpinSet("NorthWind Orders");
//Import orders records from your SQL Server
try
{
//Open the connection
connection.Open( );
//Get a sql reader from the query
System.Data.SqlClient.SqlDataReader sqlReader =
command.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
if(sqlReader != null)
{
while(sqlReader.Read( ))
{
//Get the name of the customer
string customername = sqlReader["ContactName"] as String;
//Get the company name
string companyname = sqlReader["CompanyName"] as String;
//Get the address string
string address = sqlReader["Address"] + ", " + sqlReader["City"] + ",
" + sqlReader["Region"] + ", " +
sqlReader["PostalCode"] + " " + sqlReader["Country"];
//Find location
MapPoint.Location location = null;
try
{
//Find the address
MapPoint.FindResults findrs = map.FindResults(address);
//If no results found, skip to next record
if(findrs == null || findrs.Count <= 0)
throw new Exception(address);
//Get the location
location = findrs.get_Item(ref index) as MapPoint.Location;
//Create a pushpin
if(location != null)
{
MapPoint.Pushpin pushpin =
map.AddPushpin(location, companyname);
//Assign the contact name
pushpin.Note = "Contact : " + customername;
//Move to the pushpin dataset
pushpin.MoveTo(dataset);
}
}
catch
{
//Do some logging
}
}
//Close the reader; this will automatically close the connection
//due to the command behavior setting during the
//ExecuteReader method
sqlReader.Close( );
}
}
catch
{
//Do clean up
}DisplayDataMap do not belong to shape category, since there is no way for you to either query or alter the circle shape appearance. Now, let's see how to work with shapes in MapPoint 2004.Shapes collection (similar to the DataSets collection). Using this Shapes collection, you can add a new shape or retrieve an existing shape. To add a new shape, the Shapes collection offers Add
XXXX methods (where XXXX can be a line, text box, polyline, and so on). Table 3-8 provides a list of these methods and their descriptions:|
Methodname
|
Notes
|
|---|---|
AddDrivetimeZone
|
Adds a freeform closed shape representing the driving distance from a point on the map within a specified amount of time
|
AddLine
|
Adds a new line to the map between two points
|
AddPolyline
|
DataSets object is used to import territories. The DataSets object provides the ImportTerritories method to import territories from any source supported by the ImportData method.
//Using the MapPoint Sample Territory data from txt file
string filePath = @"C:\MapPointData\2004Elections.xls";
//Define fields
object missing = System.Reflection.Missing.Value;
//Import data and create a dataset
MapPoint.DataSet dataset =
map.DataSets.ImportTerritories(filePath, missing,
MapPoint.GeoCountry.geoCountryUnitedStates,
MapPoint.GeoDelimiter.geoDelimiterDefault,
MapPoint.GeoImportFlags.geoImportExcelSheet);
//Zoom to the territory map
dataset.ZoomTo( );
Map.ObjectsFromPoint and Dataset.QueryCircle methods, in MapPoint 2004.Datasets.ImportDataDataSets and Shapes collections can be among the most interesting tasks of the MapPoint APIs. These two objects provide the core of the location-based business intelligence processing power within MapPoint 2004.