如果你還不了解微軟的MapPoint相關(guān)產(chǎn)品和服務(wù),建議去看一下MSDN上的《MapPoint 2004 與 MapPoint Web 服務(wù),該使用哪一個》這篇文章,這里為了給讀者一個初步印象,引用其中的一段話。本文介紹的是如何結(jié)合.NET開發(fā)環(huán)境開發(fā)基于MapPoint 2004的應(yīng)用。
MSDN中關(guān)于MapPoint 2004的敘述:
MapPoint 2004 是一個桌面地圖信息工具,它可以提供奇妙和豐富的用戶體驗,具有專題圖形、區(qū)域管理、路線優(yōu)化和人口統(tǒng)計數(shù)據(jù)等功能。所有必要的數(shù)據(jù)都安裝在本地,因此不需要網(wǎng)絡(luò)連接。對于 MapPoint 2004,您可以方便地使用多種常見格式(Microsoft Excel、Microsoft Access、開放數(shù)據(jù)庫連接 (ODBC) 等)導(dǎo)入數(shù)據(jù),并使用專題圖形(如圖 1 所示的餅形圖)以圖形方式顯示這些信息。
使用 MapPoint,您可以采用若干種開發(fā)方式:
·創(chuàng)建 COM 外接程序以擴展 MapPoint 的功能。
·使用 MapPoint 附帶的 ActiveX 控件將圖形嵌入到您自己的 Microsoft Visual Basic 應(yīng)用程序中。
·在其他應(yīng)用程序(例如,Microsoft Word 或 Excel)中使用 Microsoft Visual Basic for Applications 自動實現(xiàn) MapPoint 和其他應(yīng)用程序之間的連接。
·使用 Visual Basic(或任何其他與 COM 兼容的編程語言)創(chuàng)建自動執(zhí)行 MapPoint 的可執(zhí)行文件或動態(tài)鏈接庫 (DLL)。
以上內(nèi)容節(jié)選自MSDN。
正文 簡介 MapPoint 2004給程序員提供了豐富的對象模型來開發(fā)強大的商業(yè)化的智能地圖定位應(yīng)用。不過它是基于COM的思想設(shè)計的,所以如果你使用.NET Framework來編寫應(yīng)用程序的話,你必須結(jié)合使用COM的類庫。
本文通過開發(fā)一個地址查找程序,講解了如何一步步是使用.NET Framework來開發(fā)基于MapPoint 2004的應(yīng)用,同時也介紹了開發(fā)時需要注意的一些地方和解決方法。
使用MapPoint2004編程 就像前文所說,你必須結(jié)合使用MapPoint COM庫,才能使用微軟的.NET Framework進(jìn)行編程。如果你使用Visual Studio .NET,你可以在創(chuàng)建新工程之后,選擇project樹型列表項中的Add Reference選項來添加:
當(dāng)看到Add Reference對話框窗口出現(xiàn)時,選擇COM的Tab頁,并且從列表中選擇Microsoft MapPoint 11.0 Object Library ( )來添加一個對MapPoint2004類型庫的引用,如下:
下面,開始編寫C#程序,首先需要引進(jìn)MapPoint 2004命名空間:
//Add MapPoint namespace using MapPoint; |
引入命名空間之后,使用起MapPoint類型就非常方便了。
下一步是創(chuàng)建一個MapPoint 2004的應(yīng)用對象:
//Define an application instance ApplicationClass app = null; //Create an application class instance app = new ApplicationClass(); |
MapPoint 2004應(yīng)用實例(application instance)提供了一個活動的地圖實例來完成面向地圖定位的人物,這個例子里我將使用這個實例來查找一個地址。
//Now get the location FindResults frs = app.ActiveMap.FindAddressResults(" ", " ", string.Empty, "WA", "", null); |
你可能已經(jīng)注意到了,F(xiàn)indAddressResults方法返回的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訪問方法來用索引獲得地點。這個方法在你需要獲得某個特殊項但又不想循環(huán)整個列表時比較有用。如查找第一個相符合的記錄:
//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方法進(jìn)行按照索引對記錄進(jìn)行的操作。
最后當(dāng)你做完上述的操作之后,你必須記住要關(guān)閉應(yīng)用程序?qū)ο?,來確保MapPoint 2004的應(yīng)用程序?qū)嵗粫A粼趦?nèi)存中,可以使用如下代碼:
//Quit the application if(app != null) app.Quit(); app = null; |
以下代碼完整的列出了一個根據(jù)上文的方法,查找地址的函數(shù):
//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; } } } |
需要注意的地方 使用可選參數(shù)的方法進(jìn)行編程
當(dāng)你使用有可選參數(shù)的方法時,如果你沒有傳遞有效的參數(shù),你必須使用缺失類型System.Reflection.Missing.Value。比如DisplayDataMap函數(shù)如下:
DisplayDataMap([DataMapType], [DataField], [ShowDataBy], [CombineDataBy], [DataRangeType], [DataRangeOrder], [ColorScheme], [DataRangeCount], [ArrayOfCustomValues], [ArrayOfCustomNames], [DivideByField], [ArrayOfDataFieldLabels], [ArrayOfPushpinSymbols]) |
可以看到所有的參數(shù)都是可選參數(shù),這里就必須用到.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); |
結(jié)束語 以上簡單介紹了使用MapPoint 2004及在.NET開發(fā)環(huán)境下編程的一些知識,如果有興趣使用MapPoint 2004進(jìn)行地圖相關(guān)的開發(fā),可以去參考MSDN和http://www.mp2kmag.com/網(wǎng)站上的相關(guān)教程。