ArcGIS API for Silverlight-读出xml文件中的动态地图(ArcGISDynamicMapServiceLayer)

想要读出xml文件中的地图,上代码看好了:

config.xml:

<?xml version="1.0" encoding="utf-8"?>
<Config>
  <!--图形服务-->
  <Layers>
    <Layer ID="2" Type="ArcGISDynamicMapServiceLayer" Url="http://192.168.20.23/ArcGIS/rest/services/WLNZYDK/MapServer" />
    <Layer ID="3" Type="ArcGISDynamicMapServiceLayer" Url="http://192.168.20.23/ArcGIS/rest/services/WLGDDK/MapServer"/>
  </Layers>
</Config>

MainPage.xaml:

<esri:Map x:Name="Map"  />

MainPage.xaml.cs:

 public void OnLoadConfigComplete()
        {
            if (LoadConfigComplete != null)
                LoadConfigComplete();
        }

        public XDocument ConfigDoc
        {
            get;
            set;
        }

        private List<ZHPlatform.Layer.LayerItem> layerItems;

        public List<LayerItem> LayerItems
        {
            get { return layerItems; }
            set { layerItems = value; }
        }

      public MainPage()
        {
            InitializeComponent();
            //读xml
            string sourceUrl = Application.Current.Host.Source.ToString();
            int removeIndex = sourceUrl.IndexOf("ClientBin");
            string configUrl = sourceUrl.Remove(removeIndex, sourceUrl.Length - removeIndex);
            Uri configUri = new Uri(configUrl);
            Uri url = new Uri(configUri + "ClientBin/Config.xml?" + Guid.NewGuid().ToString());
            WebClient webClient = new WebClient();
            webClient.OpenReadAsync(url);
            webClient.OpenReadCompleted += ConfigLoadCompleted;
        }

        void ConfigLoadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            Stream stream = e.Result;
            using (XmlReader xReader = XmlReader.Create(stream))
            {
                XDocument document = XDocument.Load(xReader);
                ConfigDoc = document;
                //读取图层列表
                var layers = from f in document.Descendants("Layer")
                             select new ZHPlatform.Layer.LayerItem
                             {
                                 ID = f.Attribute("ID").Value,
                                  Url = f.Attribute("Url").Value,
                                 Type = (LayerType)Enum.Parse(typeof(LayerType), f.Attribute("Type").Value, false),
                             };
                if (layerItems == null)
                    layerItems = new List<LayerItem>();
                layerItems.AddRange(layers);
                MyList.ItemsSource = layerItems;
                Map.Layers = new ESRI.ArcGIS.Client.LayerCollection();
                //遍历txt中的layer添加到地图中
                foreach (LayerItem litem in layerItems)
                {
                    ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer dynamicServiceLayer = new ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer();
                    dynamicServiceLayer.Url = litem.Url;
                    dynamicServiceLayer.ID = litem.ID;
                    Map.Layers.Add(dynamicServiceLayer);
                }
                OnLoadConfigComplete();
            }
        }

LayerItem.cs

 public class LayerItem
    {        
        /// <summary>
        /// 图层ID
        /// </summary>
        public string ID
        {
            get;
            set;
        }

        private string m_Url = string.Empty;
        /// <summary>
        /// 图层地址
        /// </summary>
        public string Url
        {
            get
            {
                m_Url = WebUtil.ReDirectDynamicUrl(m_Url, string.Empty);
                return m_Url;
            }
            set { m_Url = value; }
        }
}

 

 

 

 

转载于:https://www.cnblogs.com/gezimu/archive/2013/04/25/3041961.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值