IMap map = null; //跟map同一層次的activeView對象,他們都是「地圖」的對象,map管理地圖內容,activeView管理顯示內容 IActiveView activeView = map as IActiveView; //獲取當前地圖顯示範圍 IEnvelope extent = activeView.Extent; //設置當前地圖顯示範圍,至關於用代碼實現縮放到某個範圍 activeView.Extent = extent; //獲取地圖全圖範圍 //地圖瀏覽功能中的全圖,他的範圍就是這樣獲取 IEnvelope fullExtent = activeView.FullExtent; //獲取地圖比例尺,同時可同於設置比例尺 double mapScale = map.MapScale; //獲取和設置地圖單位 esriUnits unit = map.MapUnits; //獲取和設置地圖的顯示單位 esriUnits unit2 = map.DistanceUnits; //刷新地圖 activeView.Refresh(); //屏幕長度(單位是像素)轉地圖實際長度 double mapLength = MapHelper.PixelsToMapUnits(activeView, 5); //獲取Map的全部FeatureLayer List<IFeatureLayer> lstFeatureLayer = MapHelper.GetAllFeatureLayerInMap(map); if (1 == 2) { //在Map中經過真實表名獲取圖層,對於sde的表名不能帶用戶名 IFeatureLayer featureLayer = MapHelper.GetFeatureLayerByDatasetName(map, "roadLine"); } foreach (IFeatureLayer featureLayer in lstFeatureLayer) { //圖層相關說明 //IFeatureLayer表明矢量圖層,而ILayer表明圖層,也是全部類型圖層接口都實現了ILayer,所以IFeatureLayer能夠as到ILayer ILayer layer = featureLayer as ILayer; //獲取和設置圖層的可視狀態(就是圖層樹界面裏圖層左邊的checkbox) bool visible = layer.Visible; //獲取或設置圖層名稱 string name = layer.Name; //經過圖層獲取featureClass,是獲取featureClass的方法之一。另外一種方法是經過workspace獲取 //注意IFeatureLayer和IFeatureClass是兩個東西,雖然平時會都叫「圖層」。IFeatureClass是指物理表,而IFeatureLayer指物理表加載到地圖上造成的圖層,前者更多指數據,後者只圖層在地圖的展現設置,如樣式,標註等 IFeatureClass featureClass = featureLayer.FeatureClass; }
調用過的封裝函數代碼以下函數
public static double PixelsToMapUnits(IActiveView activeView, double pixelLength) { tagRECT tagRect = activeView.ScreenDisplay.DisplayTransformation.get_DeviceFrame(); int width = tagRect.right - tagRect.left; if (width < 0.00001) return pixelLength; double scale = activeView.ScreenDisplay.DisplayTransformation.VisibleBounds.Width / width; return (pixelLength * scale); } public static List<IFeatureLayer> GetAllFeatureLayerInMap(IMap map) { List<IFeatureLayer> result = new List<IFeatureLayer>(); UID uid = new UIDClass(); uid.Value = "{40A9E885-5533-11d0-98BE-00805F7CED21}"; //FeatureLayer IEnumLayer enumLayer = map.get_Layers(uid, true); //對圖層轉換版本 ILayer layer = null; while ((layer = enumLayer.Next()) != null) { IFeatureLayer featureLayer = layer as IFeatureLayer; result.Add(featureLayer); } return result; } public static IFeatureLayer GetFeatureLayerByDatasetName(IMap map, string tableName) { List<IFeatureLayer> lstFeatureLayer = GetAllFeatureLayerInMap(map); foreach (IFeatureLayer featureLayer in lstFeatureLayer) { if (featureLayer.FeatureClass != null) { string featureClassName = FeatureClassHelper.GetDatasetNameWithOutSDEUser(featureLayer.FeatureClass as IDataset); if (string.Equals(featureClassName, tableName, StringComparison.OrdinalIgnoreCase)) { return featureLayer; } } } return null; } public static string GetDatasetNameWithOutSDEUser(IDataset dataset) { if (dataset == null) return ""; string tDatasetName = dataset.Name; if (tDatasetName.Contains(".")) { tDatasetName = tDatasetName.Substring(tDatasetName.LastIndexOf(".") + 1); } return tDatasetName; }