利用ArcEngine加载ArcGIS Server中的地图服务需要用到三个参数:

1、服务器地址

2、地图服务名称

3、局域网连接还是Internet连接

下面以ArcGIS Server自带的地图服务作为演示:

ArcEngine加载ArcGIS Server地图服务_System


服务器地址:http://localhost:6080/arcgis/admin

服务名称:SampleWorldCities

连接方式:Inetrnet连接

ArcEngine加载ArcGIS Server地图服务_System_02


代码如下 :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.GISClient;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.SystemUI;

namespace DXApplication1
{
    public partial class Form1 : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        public Form1()
        {
            InitializeComponent();
            axTOCControl1.SetBuddyControl(axMapControl1.Object);
        }

        /// <summary>
        /// 家在地图服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            LoadMapServer("http://localhost:6080/arcgis/admin", "SampleWorldCities", false);
        }

        /// <summary>
        /// 加载ArcGIS地图服务
        /// </summary>
        /// <param name="hostOrUrl">服务器地址</param>
        /// <param name="serviceName">服务名称</param>
        /// <param name="isLAN">是否为局域网连接</param>
        private void LoadMapServer(string hostOrUrl, string serviceName, bool isLAN)
        {
            IPropertySet pPropertySet = new PropertySet();
            if (isLAN)
            {
                pPropertySet.SetProperty("machine", hostOrUrl);
            }
            else
            {
                pPropertySet.SetProperty("url", hostOrUrl);
            }

            // 连接服务器
            IAGSServerConnectionFactory pAGSServerConnectionFactory = new AGSServerConnectionFactory();
            IAGSServerConnection pAGSServerConnection = pAGSServerConnectionFactory.Open(pPropertySet, 0);

            // 获取服务
            IAGSEnumServerObjectName pAGSEnumServerObjectName = pAGSServerConnection.ServerObjectNames;
            pAGSEnumServerObjectName.Reset();
            IAGSServerObjectName pAGSServerObjectName = pAGSEnumServerObjectName.Next();
            while (pAGSServerObjectName != null)
            {
                if (pAGSServerObjectName.Name.ToLower() == serviceName.ToLower() && pAGSServerObjectName.Type == "MapServer")
                {
                    break;
                }
                pAGSServerObjectName = pAGSEnumServerObjectName.Next();
            }

            // 获取服务图层
            IAGSServerObject pAGSServerObject = (pAGSServerObjectName as IName).Open() as IAGSServerObject;
            IMapServer pMapServer = pAGSServerObject as IMapServer;
            IMapServerLayer pMapServerLayer = new MapServerLayer() as IMapServerLayer;
            pMapServerLayer.ServerConnect(pAGSServerObjectName, pMapServer.DefaultMapName);

            // 添加数据图层
            axMapControl1.AddLayer(pMapServerLayer as ILayer);
            axMapControl1.Refresh();
        }
    }
}

结果如下:

ArcEngine加载ArcGIS Server地图服务_System_03