Section 3.7. Working with Territories


3.7. Working with Territories

MapPoint 2004 provides APIs to generate and display territories based on external data (such as a text file or Access database). As with importing any other location data, the 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.

The following code shows how to create a territory map using the 2004 U.S. presidential election results:

     //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( );

The previous code yields the territory map shown in Figure 3-18.

Figure 3-18. U.S. presidential election results as a territory map


Territories are one of the most interesting features of MapPoint 2004; however, they are also a feature that has serious limitations in terms of what you can do with the MapPoint 2004 APIs. For example, if you want to query a location to determine which territory it belongs to, there is no API that can be used for this purpose. However, there is a workaround to achive this.

3.7.1. Determining a Location's Territory

While there is no territory query API available in MapPoint 2004, you can determine a location's territory using a combination of other APIs, specifically Map.ObjectsFromPoint and Dataset.QueryCircle methods, in MapPoint 2004.

Say, for example, you are given the location "Redmond, WA," and now you have to determine whose sales territory it belongs to. In order to do that, you must import the territories into a dataset using the Datasets.ImportData method:

     //Import territories as Data     //Using the MapPoint Sample Territory data from Xls file     string filePath = @"C:\SampleTerritories.xls";     //Define fields     object missing = System.Reflection.Missing.Value;     MapPoint.DataSet dataset =           map.DataSets.ImportData(filePath, missing,           MapPoint.GeoCountry.geoCountryUnitedStates,           MapPoint.GeoDelimiter.geoDelimiterDefault,           MapPoint.GeoImportFlags.geoImportExcelSheet);

Now that your territories are available in a dataset, find the input location using the Map.FindResults method:

     //Now find input place     object index = 1;       MapPoint.Location loc =          map.FindResults("Redmond, WA").get_Item(ref index)          as MapPoint.Location;

Using this location, use the hit-detection method to find objects from the corresponding point:

     //Get objects from the given point     MapPoint.FindResults findres =      map.ObjectsFromPoint(map.LocationToX(loc), map.LocationToY(loc));

For each of these objects, see whether there is any territory-representing location present in the dataset that you created at the beginning:

     //Now for each object see if there is any territory representing     //location in the dataset     for(int i=0; i<findres.Count;i++)     {        object ind = i+1;        MapPoint.Location loc1            = findres.get_Item(ref ind) as MapPoint.Location;       if(loc1.Type != MapPoint.GeoShowDataBy.geoShowByRegion1)          continue;       double radius = 1;       bool notfound = true;       object fieldIndex = 2;       while(notfound)       {         //Query the dataset         MapPoint.Recordset recs              = dataset.QueryCircle(loc1, radius);         while(!recs.EOF)          {             //Found territory!             MessageBox.Show(recs.Fields.get_Item(ref fieldIndex).Value.ToString( ));             notfound = false;             i = findres.Count + 1;             break;          }           if(radius > 30)            {                //For performance and accuracy reasons                //Don't search any further                //Failed to find the territory                break;            }           else            {              //Increment the radius and search               //Dataset again               radius = radius + 5;            }         }       }

All that happens in this code is a query of the dataset around each location (returned by the ObjectsFromPoint method) using the QueryCircle method by increasing the radius until we find a territory or until the radius execeeds 30 miles (a hypothetical value that can be increased or reduced depending on your territory locations). This technique works for most of the territories except when they are too small (for example, if you find multiple territories in a 1-mile radius).




Programming MapPoint in  .NET
Programming MapPoint in .NET
ISBN: 0596009062
EAN: 2147483647
Year: 2005
Pages: 136
Authors: Chandu Thota

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net