下一步是创建一个MapPoint 2004的应用对象:
//Define an application instance
ApplicationClass app = null;
//Create an application class instance
app = new ApplicationClass();
MapPoint 2004应用实例(application instance)提供了一个活动的地图实例来完成面向地图定位的人物,这个例子里我将使用这个实例来查找一个地址。
//Now get the location
FindResults frs = app.ActiveMap.FindAddressResults(" ", " ", string.Empty, "WA", "", null);
你可能已经注意到了,FindAddressResults方法返回的FindResults是一个查询到的地点的集合,有两种方法可以从FindResults实例中获取地点的列表:
1. 获取一个enumerator并且枚举整个地点的列表。如果你想提供符合条件的地址的列表这样的方式比较有用。
//Get an enumerator
IEnumerator ienum = frs.GetEnumerator();
//Loop through the enumerator
while(ienum.MoveNext())
{
Location loc = ienum.Current as Location;
if(loc != null)
{
//process the location
string s = loc.StreetAddress.Value;
}
}
2. 使用get/set访问方法来用索引获得地点。这个方法在你需要获得某个特殊项但又不想循环整个列表时比较有用。如查找第一个相符合的记录:
//Define an index
object index = 1;
//Access the location item using the accessor method
location = frs.get_Item(ref index) as Location;
这里必须使用get_Item和set_Item方法进行按照索引对记录进行的操作。
最后当你做完上述的操作之后,你必须记住要关闭应用程序对象,来确保MapPoint 2004的应用程序实例不会保留在内存中,可以使用如下代码:
//Quit the application
if(app != null)
app.Quit();
app = null;
以下代码完整的列出了一个根据上文的方法,查找地址的函数:
//Define an application instance
ApplicationClass app = null;
//Define a location instance
Location location = null;
//Define a FindResults instance
FindResults frs = null;
try
{
//Create an application class
app = new ApplicationClass();
//Now get the location
frs = app.ActiveMap.FindAddressResults(" ", " "
, string.Empty, "WA", "", null);
//Check if the find query is succesfull
if(frs != null && frs.Count > 0)
{
object index = 1;
location = frs.get_Item(ref index) as Location;
//Male the MapPoint 2004 application visible
//and go to that location
app.Visible = true;
location.GoTo();
//Do your processing with the location
MessageBox.Show(location.StreetAddress.Value);
}
}
catch(Exception ex)
{
string message = ex.Message;
}
finally
{
if(app != null)
{
try
{
app.Quit();
}
catch
{
//This means your app has already quit!
}
finally
{
app = null;
}
}
}
需要注意的地方
当你使用有可选参数的方法时,如果你没有传递有效的参数,你必须使用缺失类型System.Reflection.Missing.Value。比如DisplayDataMap函数如下:
DisplayDataMap([DataMapType], [DataField], [ShowDataBy], [CombineDataBy], [DataRangeType], [DataRangeOrder], [ColorScheme], [DataRangeCount], [ArrayOfCustomValues], [ArrayOfCustomNames], [DivideByField], [ArrayOfDataFieldLabels], [ArrayOfPushpinSymbols])
可以看到所有的参数都是可选参数,这里就必须用到.NET Framework里的缺失类型了。下面的代码展示了如何使用:
//Define a missing value type
object missing = System.Reflection.Missing.Value;
//Use the missing type for optional parameters that are not needed
DataMap mydatamap =
mydataset.DisplayDataMap(GeoDataMapType.geoDataMapTypeShadedArea,
field, GeoShowDataBy.geoShowByRegion1,
GeoCombineDataBy.geoCombineByDefault,
GeoDataRangeType.geoRangeTypeDiscreteLogRanges,
GeoDataRangeOrder.geoRangeOrderDefault, 15, 3,
missing, missing, missing, missing, missing);
结束语
以上简单介绍了使用MapPoint 2004及在.NET开发环境下编程的一些知识,如果有兴趣使用MapPoint 2004进行地图相关的开发,可以去参考MSDN和http://www.mp2kmag.com/网站上的相关教程。