天天看點

ArcEngine在地圖上加載Server圖層資料

版權聲明:歡迎評論和轉載,轉載請注明來源。 https://blog.csdn.net/zy332719794/article/details/22183775

        加載Server圖層資料需要指定兩個參數,第一是服務的Url位址,第二是服務中的資料對象名稱Name。也就是說,一個Url服務中包含了若幹個資料對象,我們加載時可以通過名稱加載的資料對象,當然也可以周遊将其全部加上。

例:加載服務位址"http://services.arcgisonline.com/ArcGIS/services"中的"ESRI_Imagery_World_2D"資料對象(圖層)到地圖上。

示例代碼:

private void GetServerTest()
        {
            //獲得服務對象名稱
            IAGSServerObjectName serverObjectName =GetMapServer(
                "http://services.arcgisonline.com/ArcGIS/services", "ESRI_Imagery_World_2D", false);
            IName pName = (IName)serverObjectName;
            //通路地圖服務
            IAGSServerObject serverObject = (IAGSServerObject)pName.Open();
            IMapServer mapServer = (IMapServer)serverObject;

            IMapServerLayer mapServerLayer = new MapServerLayer() as IMapServerLayer;
            
            //連接配接地圖服務
            mapServerLayer.ServerConnect(serverObjectName, mapServer.DefaultMapName);
            
            //添加資料圖層
            _application.MapControl.AddLayer(mapServerLayer as ILayer);
            _application.MapControl.Refresh();
        } 

        public IAGSServerObjectName GetMapServer(string pHostOrUrl, string pServiceName, bool pIsLAN)
        {        
            //設定連接配接屬性
            IPropertySet propertySet = new PropertySetClass();
            if (pIsLAN)
                propertySet.SetProperty("machine", pHostOrUrl);
            else
                propertySet.SetProperty("url", pHostOrUrl);

            //打開連接配接
            IAGSServerConnectionFactory factory = new AGSServerConnectionFactory();            
            IAGSServerConnection pConnection = factory.Open(propertySet, 0);

            //Get the image server.
            IAGSEnumServerObjectName pServerObjectNames = pConnection.ServerObjectNames;
            pServerObjectNames.Reset();
            IAGSServerObjectName ServerObjectName = pServerObjectNames.Next();
            while (ServerObjectName != null)
            {
                if ((ServerObjectName.Name.ToLower() == pServiceName.ToLower()) 
                    && (ServerObjectName.Type == "MapServer") )
                {                    
                    break;
                }
                ServerObjectName = pServerObjectNames.Next();
            }

            return ServerObjectName;
        }           

下面附上一張效果圖:

繼續閱讀