代码
public List<ILayer> GetWMTSLayer(string sUrl)
{
var lstLayer = new List<ILayer>();
try
{
IPropertySet pPropertySet = new PropertySetClass();
pPropertySet.SetProperty("URL", sUrl);
IWMTSConnectionFactory pWmtsConnectionFactory = new WMTSConnectionFactoryClass();
IWMTSConnection pWmtsConnection = pWmtsConnectionFactory.Open(pPropertySet, 0, null);
IWMTSServiceDescription pWmtsServiceDescription = pWmtsConnection as IWMTSServiceDescription;
for (int i = 0; i < pWmtsServiceDescription.LayerDescriptionCount; i++)
{
IWMTSLayerDescription pWmtsLayerDescription = pWmtsServiceDescription.LayerDescription[i];
IWMTSLayer pWmtsLayer = new WMTSLayerClass();
IPropertySet pPropSet = new PropertySetClass();
pPropSet.SetProperty("URL", sUrl);
pPropSet.SetProperty("LayerName", pWmtsLayerDescription.Identifier);
WMTSConnectionName connectionName = new WMTSConnectionNameClass
{
ConnectionProperties = pPropSet
};
if (pWmtsLayer.Connect((IName)connectionName))
{
lstLayer.Add(pWmtsLayer as ILayer);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return lstLayer;
}
说明
- 不要在子线程中调用,否则会在pWmtsLayer.Connect((IName)connectionName)处报错。