服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

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


  如果你还不了解微软的mappoint相关产品和服务,建议去看一下msdn上的《mappoint 2004 与 mappoint web 服务,该使用哪一个》这篇文章,这里为了给读者一个初步印象,引用其中的一段话。本文介绍的是如何结合.net开发环境开发基于mappoint 2004的应用。
  
  msdn中关于mappoint 2004的叙述:
  
  mappoint 2004 是一个桌面地图信息工具,它可以提供奇妙和丰富的用户体验,具有专题图形、区域管理、路线优化和人口统计数据等功能。所有必要的数据都安装在本地,因此不需要网络连接。对于 mappoint 2004,您可以方便地使用多种常见格式(microsoft excel、microsoft access、开放数据库连接 (odbc) 等)导入数据,并使用专题图形(如图 1 所示的饼形图)以图形方式显示这些信息。
  
 微软地图mappoint2004编程简介(图)(图一)

  
mappoint 2004

  
  使用 mappoint,您可以采用若干种开发方式:
  
  ?创建 com 外接程序以扩展 mappoint 的功能。
  
  ?使用 mappoint 附带的 activex 控件将图形嵌入到您自己的 microsoft visual basic 应用程序中。
  
  ?在其他应用程序(例如,microsoft word 或 excel)中使用 microsoft visual basic for applications 自动实现 mappoint 和其他应用程序之间的连接。
  
  ?使用 visual basic(或任何其他与 com 兼容的编程语言)创建自动执行 mappoint 的可执行文件或动态链接库 (dll)。
  
  以上内容节选自msdn。
  
  正文
  
  简介
  
  mappoint 2004给程序员提供了丰富的对象模型来开发强大的商业化的智能地图定位应用。不过它是基于com的思想设计的,所以如果你使用.net framework来编写应用程序的话,你必须结合使用com的类库。
  
  本文通过开发一个地址查找程序,讲解了如何一步步是使用.net framework来开发基于mappoint 2004的应用,同时也介绍了开发时需要注意的一些地方和解决方法。
  
  使用mappoint2004编程
  
  就像前文所说,你必须结合使用mappoint com库,才能使用微软的.net framework进行编程。如果你使用visual studio .net,你可以在创建新工程之后,选择project树型列表项中的add reference选项来添加:
  
 微软地图mappoint2004编程简介(图)(图二)

  
mappoint 2004

  
  当看到add reference对话框窗口出现时,选择com的tab页,并且从列表中选择microsoft mappoint 11.0 object library ( )来添加一个对mappoint2004类型库的引用,如下:
  
微软地图mappoint2004编程简介(图)(图三)

  
mappoint 2004

  
  下面,开始编写c#程序,首先需要引进mappoint 2004命名空间:
  
  //add mappoint namespace
  using mappoint;
  
  引入命名空间之后,使用起mappoint类型就非常方便了。
  
  下一步是创建一个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);

扫描关注微信公众号