国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

.NET中微軟地圖MapPoint2004編程簡介

2019-11-17 04:59:07
字體:
來源:轉載
供稿:網友
引言

  假如你還不了解微軟的MapPoint相關產品和服務,建議去看一下MSDN上的《MapPoint 2004 與 MapPoint Web 服務,該使用哪一個》這篇文章,這里為了給讀者一個初步印象,引用其中的一段話。本文介紹的是如何結合.NET開發環境開發基于MapPoint 2004的應用。

  MSDN中關于MapPoint 2004的敘述:

  MapPoint 2004 是一個桌面地圖信息工具,它可以提供奇妙和豐富的用戶體驗,具有專題圖形、區域治理、路線優化和人口統計數據等功能。所有必要的數據都安裝在本地,因此不需要網絡連接。對于 MapPoint 2004,您可以方便地使用多種常見格式(Microsoft Excel、Microsoft access、開放數據庫連接 (ODBC) 等)導入數據,并使用專題圖形(如圖 1 所示的餅形圖)以圖形方式顯示這些信息。

.NET中微軟地圖MapPoint2004編程簡介(圖一)

  使用 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選項來添加:

.NET中微軟地圖MapPoint2004編程簡介(圖二)

  當看到Add Reference對話框窗口出現時,選擇COM的Tab頁,并且從列表中選擇Microsoft MapPoint 11.0 Object Library ( )來添加一個對MapPoint2004類型庫的引用,如下:

.NET中微軟地圖MapPoint2004編程簡介(圖三)

  下面,開始編寫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);
  結束語

  以上簡單介紹了使用MapPoint 2004及在.NET開發環境下編程的一些知識,假如有愛好使用MapPoint 2004進行地圖相關的開發,可以去參考MSDN和http://www.mp2kmag.com/網站上的相關教程。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 壤塘县| 衡阳县| 潮州市| 广德县| 泰安市| 信阳市| 建始县| 阿鲁科尔沁旗| 南郑县| 从江县| 蒙阴县| 南皮县| 沭阳县| 剑阁县| 富裕县| 香河县| 丰台区| 海城市| 和龙市| 张家界市| 吉木萨尔县| 视频| 黑水县| 金平| 博白县| 临猗县| 辉县市| 科尔| 海阳市| 策勒县| 辛集市| 会昌县| 岑溪市| 卢龙县| 峨边| 车致| 平度市| 宜宾县| 克什克腾旗| 扶风县| 南投县|