电脑故障

位置:IT落伍者 >> 电脑故障 >> 浏览文章

微软地图MapPoint2004编程简介(图)


发布日期:2021/7/8
 

如果你还不了解微软的MapPoint相关产品和服务建议去看一下MSDN上的《MapPoint 与 MapPoint Web 服务该使用哪一个》这篇文章这里为了给读者一个初步印象引用其中的一段话本文介绍的是如何结合NET开发环境开发基于MapPoint 的应用

MSDN中关于MapPoint 的叙述

MapPoint 是一个桌面地图信息工具它可以提供奇妙和丰富的用户体验具有专题图形区域管理路线优化和人口统计数据等功能所有必要的数据都安装在本地因此不需要网络连接对于 MapPoint 您可以方便地使用多种常见格式(Microsoft ExcelMicrosoft Access开放数据库连接 (ODBC) 等)导入数据并使用专题图形(如图 所示的饼形图)以图形方式显示这些信息

MapPoint

使用 MapPoint您可以采用若干种开发方式

·创建 COM 外接程序以扩展 MapPoint 的功能

·使用 MapPoint 附带的 ActiveX 控件将图形嵌入到您自己的 Microsoft Visual Basic 应用程序中

·在其他应用程序(例如Microsoft Word 或 Excel)中使用 Microsoft Visual Basic for Applications 自动实现 MapPoint 和其他应用程序之间的连接

·使用 Visual Basic(或任何其他与 COM 兼容的编程语言)创建自动执行 MapPoint 的可执行文件或动态链接库 (DLL)

以上内容节选自MSDN

正文

简介

MapPoint 给程序员提供了丰富的对象模型来开发强大的商业化的智能地图定位应用不过它是基于COM的思想设计的所以如果你使用NET Framework来编写应用程序的话你必须结合使用COM的类库

本文通过开发一个地址查找程序讲解了如何一步步是使用NET Framework来开发基于MapPoint 的应用同时也介绍了开发时需要注意的一些地方和解决方法

使用MapPoint编程

就像前文所说你必须结合使用MapPoint COM库才能使用微软的NET Framework进行编程如果你使用Visual Studio NET你可以在创建新工程之后选择project树型列表项中的Add Reference选项来添加

MapPoint

当看到Add Reference对话框窗口出现时选择COM的Tab页并且从列表中选择Microsoft MapPoint Object Library ( )来添加一个对MapPoint类型库的引用如下

MapPoint

下面开始编写C#程序首先需要引进MapPoint 命名空间

//Add MapPoint namespace

using MapPoint;

引入命名空间之后使用起MapPoint类型就非常方便了

下一步是创建一个MapPoint 的应用对象

//Define an application instance

ApplicationClass app = null;

//Create an application class instance

app = new ApplicationClass();

MapPoint 应用实例(application instance)提供了一个活动的地图实例来完成面向地图定位的人物这个例子里我将使用这个实例来查找一个地址

//Now get the location

FindResults frs = appActiveMapFindAddressResults( stringEmpty WA null);

你可能已经注意到了FindAddressResults方法返回的FindResults是一个查询到的地点的集合有两种方法可以从FindResults实例中获取地点的列表

获取一个enumerator并且枚举整个地点的列表如果你想提供符合条件的地址的列表这样的方式比较有用

//Get an enumerator

IEnumerator ienum = frsGetEnumerator();

//Loop through the enumerator

while(ienumMoveNext())

{

Location loc = ienumCurrent as Location;

if(loc != null)

{

//process the location

string s = locStreetAddressValue;

}

}

使用get/set访问方法来用索引获得地点这个方法在你需要获得某个特殊项但又不想循环整个列表时比较有用如查找第一个相符合的记录

//Define an index

object index = ;

//Access the location item using the accessor method

location = frsget_Item(ref index) as Location;

这里必须使用get_Item和set_Item方法进行按照索引对记录进行的操作

最后当你做完上述的操作之后你必须记住要关闭应用程序对象来确保MapPoint 的应用程序实例不会保留在内存中可以使用如下代码

//Quit the application

if(app != null)

appQuit();

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 = appActiveMapFindAddressResults( stringEmpty WA null);

//Check if the find query is succesfull

if(frs != null && frsCount > )

{

object index = ;

location = frsget_Item(ref index) as Location;

//Male the MapPoint application visible

//and go to that location

appVisible = true;

locationGoTo();

//Do your processing with the location

MessageBoxShow(locationStreetAddressValue);

}

}

catch(Exception ex)

{

string message = exMessage;

}

finally

{

if(app != null)

{

try

{

appQuit();

}

catch

{

//This means your app has already quit!

}

finally

{

app = null;

}

}

}

需要注意的地方

使用可选参数的方法进行编程

当你使用有可选参数的方法时如果你没有传递有效的参数你必须使用缺失类型SystemReflectionMissingValue比如DisplayDataMap函数如下

DisplayDataMap([DataMapType] [DataField] [ShowDataBy] [CombineDataBy] [DataRangeType] [DataRangeOrder] [ColorScheme] [DataRangeCount] [ArrayOfCustomValues] [ArrayOfCustomNames] [DivideByField] [ArrayOfDataFieldLabels] [ArrayOfPushpinSymbols])

可以看到所有的参数都是可选参数这里就必须用到NET Framework里的缺失类型了下面的代码展示了如何使用

//Define a missing value type

object missing = SystemReflectionMissingValue;

//Use the missing type for optional parameters that are not needed

DataMap mydatamap =

mydatasetDisplayDataMap(GeoDataMapTypegeoDataMapTypeShadedArea

field GeoShowDataBygeoShowByRegion

GeoCombineDataBygeoCombineByDefault

GeoDataRangeTypegeoRangeTypeDiscreteLogRanges

GeoDataRangeOrdergeoRangeOrderDefault

missing missing missing missing missing);

上一篇:解析图片1D/2D码(条形码)

下一篇:测试概念进行代码设计时的七条基本原则