版权声明:欢迎评论和转载,转载请注明来源。 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;
}
下面附上一张效果图: