做簡單的地圖展示,不需要專業的GIS組件,使用DevExpress即可完成。以加載Shp地圖文件為例。
private void LoadShp(string path) { //創建SHP數據源 ShapefileDataAdapter dataAdapter = new ShapefileDataAdapter { FileUri = new Uri(path), SourceCoordinateSystem = new CartesianSourceCoordinateSystem(), //坐標系 DefaultEncoding = Encoding.UTF8, //編碼 }; //創建矢量圖層 VectorItemsLayer vectorItemsLayer = new VectorItemsLayer { Data = dataAdapter, Colorizer = CreateChoroplethColorizer("SCP") //分級符號化 }; vectorItemsLayer.Name = "任務區"; //標注 vectorItemsLayer.ShapeTitlesVisibility = VisibilityMode.Visible; vectorItemsLayer.ShapeTitlesPattern = "{Name}:{ SCP }%"; //設置地圖控件屬性 this.mapControl1.CoordinateSystem = new CartesianMapCoordinateSystem(); this.mapControl1.NavigationPanelOptions.Visible = false; //加載圖層 mapControl1.Layers.Add(vectorItemsLayer); //加載圖例 MapLegendBase mapLegendBase = CreateLegend(vectorItemsLayer); mapLegendBase.Alignment = LegendAlignment.TopLeft; this.mapControl1.Legends.Add(mapLegendBase); }
需要注意的問題:
(1)正確設置ShapefileDataAdapter和mapControl的坐標系統CoordinateSystem,否則無法顯示;
(2)當標注用到包含中文的屬性時會亂碼,需設置DataAdapter的默認編碼屬性DefaultEncoding;
(3)標注使用圖層的ShapeTitlesPattern屬性,它是一個字符串,對於引用的字段,需加上中括號,例如"{Name}"表示用屬性表中Name來標注 ;
(4)也支持圖例、符號化等基本地圖概念。