方法一:
1.打开连接
private void OpenConn(bool bLAN,string sval)
{
try
{
IAGSServerConnectionFactory pConnF = new AGSServerConnectionFactory();
IPropertySet pProSet = new PropertySet();
if(bLAN)
pProSet.SetProperty("machine", sval);
else
pProSet.SetProperty("url", sval);
clsPublic.pConn = pConnF.Open(pProSet, 0);
if (clsPublic.pConn != null)
{
MessageBox.Show("连接成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("连接失败","提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
catch { }
}
2.加载地图服务
public void AddMapService(AxMapControl pMapCtrl, string sServiceName, IAGSServerConnection pConn)
{
pConn.ServerObjectNames.Reset();
IAGSEnumServerObjectName pEnumServerObjectNames = pConn.ServerObjectNames;
pEnumServerObjectNames.Reset();
IAGSServerObjectName pServerObjectName = pEnumServerObjectNames.Next();
IAGSServerObjectName sServerObjectName = null;
while (pServerObjectName != null)
{
if (pServerObjectName.Name == sServiceName)
{
sServerObjectName = pServerObjectName;
break;
}
pServerObjectName = pEnumServerObjectNames.Next();
}
if (sServerObjectName == null)
{
MessageBox.Show("没有找到对应的MapService", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
IName pName = sServerObjectName as IName;
IMapServer pMapServer = pName.Open() as IMapServer;
IMapServerLayer pMSLyr = new MapServerLayerClass();
pMSLyr.ServerConnect(sServerObjectName, pMapServer.DefaultMapName);
ILayer plyr = pMSLyr as ILayer;
pMapCtrl.ClearLayers();
pMapCtrl.AddLayer(plyr);
pMapCtrl.Extent = pMapCtrl.FullExtent;
}
方法二:
IMapServerRESTLayer pRestLayer = new MapServerRESTLayerClass();
pRestLayer.Connect("http://myServer:6080/arcgis/rest/services/WaterMap2015/MapServer");
axMapControl1.AddLayer(pRestLayer as ILayer);
项目实例:
/// <summary>
/// 加载地图服务
/// </summary>
/// <param name="serverName">服务器名称</param>
/// <param name="serviceName">地图服务名称</param>
/// <returns>返回的Layer加到地图控件中</returns>
private ILayer LoadMapservice(string serverName, string serviceName)
{
IMapServer m_mapServer = null;
IMapServerLayer layer = null;
IPropertySet propertySet = new PropertySet();
//propertySet.SetProperty("url", "http://ags021183.szhy.cn/hys_szhy_basemap/rest/services");
propertySet.SetProperty("url", "http://" + serverName);
IAGSServerConnectionFactory agsServerConnectionFactory = new AGSServerConnectionFactory();
IAGSServerConnection agsServerConnection = agsServerConnectionFactory.Open(propertySet, 0);
IAGSEnumServerObjectName agsEnumServerObjectName = agsServerConnection.ServerObjectNames;
agsEnumServerObjectName.Reset();
IName serverObjectName;
IAGSServerObject agsServerObject = null;
IAGSServerObjectName agsServerObjectName = agsEnumServerObjectName.Next();
while ((agsServerObjectName != null))
{
// Search for the MapServer Server Object
if (agsServerObjectName.Type.ToUpper() == "MAPSERVER" && agsServerObjectName.Name == serviceName)
{
serverObjectName = agsServerObjectName as IName;
agsServerObject = serverObjectName.Open() as IAGSServerObject;
if (agsServerObject as IMapServer != null)
{
m_mapServer = agsServerObject as IMapServer;
break;
}
}
agsServerObjectName = agsEnumServerObjectName.Next();
}
// If we got both of them, exit out
if (m_mapServer != null)
{
layer = new MapServerLayerClass();
layer.ServerConnect(agsServerObjectName, m_mapServer.DefaultMapName);
}
if (layer != null)
{
return (layer as ILayer);
}
else
{
return null;
}
}