ArcGIS api fo silverlight学习一(silverlight加载GeoServer发布的WMS地图)

时间:2022-03-15 11:15:15

最好的学习资料ArcGIS api fo silverlight官网:http://help.arcgis.com/en/webapi/silverlight/samples/start.htm

一、GeoServer环境搭建及数据发布

参考:http://www.cnblogs.com/beniao/archive/2011/01/08/1930822.html等

二、创建ArcGIS API for SilverLight应用

参考:http://blog.csdn.net/zdw_wym/article/details/7620962

1、创建SilverLight应用程序

ArcGIS api fo silverlight学习一(silverlight加载GeoServer发布的WMS地图)

2、添加ArcGIS API for SilverLight相关引用

ArcGIS api fo silverlight学习一(silverlight加载GeoServer发布的WMS地图)

3、往MainPage.xaml中添加地图控件代码

ArcGIS api fo silverlight学习一(silverlight加载GeoServer发布的WMS地图)

四、读取GeoServer后台发布的图层

参考出处:http://blog.csdn.net/xinruogis/article/details/5567103

1、创建类文件,实现自定义wms

public class WMSMapServiceLayer : DynamicMapServiceLayer  //继承接口,实现GetUrl方法
    {
        List<string> _layers = new List<string>();
        public WMSMapServiceLayer(Envelope extent, int KWID, string url, string[] layers)
        {
            this.FullExtent = extent;    //地图范围
            this.SpatialReference = new ESRI.ArcGIS.Client.Geometry.SpatialReference(KWID);  //空间参考
            this.Url = url;     //wms地址

this.Version = "1.1.1";   //wms版本
            this.Layers = layers;    //请求的图层
           // _layers.Add("topp:states");

}

public string Url { get; set; }
        public string Version { get; set; }
        //public Envelope Extent { get; set; }
        public string[] Layers
        {
            get { return _layers.ToArray(); }
            set { _layers = new List<string>(value); OnLayerChanged(); }
        }
        //public override void Initialize()
        //{
        //   // this.FullExtent = Extent;     //设置范围(不设置就无法实现)
        //    //this.SpatialReference = new SpatialReference(KWID);
        //    base.Initialize();
         
        //}

//重写获取数据接口的方法

public override void GetUrl(ESRI.ArcGIS.Client.Geometry.Envelope extent, int width, int height, DynamicMapServiceLayer.OnUrlComplete onComplete)
        {
            int extentWKID = extent.SpatialReference.WKID;
            StringBuilder mapURL = new StringBuilder();
            mapURL.Append(Url);
            mapURL.Append("?service=WMS");
            mapURL.Append("&request=GetMap");
            mapURL.AppendFormat("&width={0}", width);
            mapURL.AppendFormat("&height={0}", height);
            mapURL.AppendFormat("&format={0}", "image/png");
            mapURL.AppendFormat("&layers={0}", String.Join(",", Layers));
            mapURL.Append("&styles=");
            mapURL.AppendFormat("&bgcolor={0}", "0xFFFFFF");
            mapURL.AppendFormat("&transparent={0}", "true");
            mapURL.AppendFormat("&version={0}", Version);
            switch (Version)
            {
                case ("1.1.1"): mapURL.AppendFormat("&SRS=EPSG:{0}", extentWKID);
                    mapURL.AppendFormat(CultureInfo.InvariantCulture, "&bbox={0},{1},{2},{3}",
                        extent.XMin, extent.YMin, extent.XMax, extent.YMax); break;
            }
            //把那些字符串拼接起来,跟openlayer一样的
            onComplete(mapURL.ToString(), width, height, new ESRI.ArcGIS.Client.Geometry.Envelope()
            {
                XMin = extent.XMin,
                YMin = extent.YMin,
                XMax = extent.XMax,
                YMax = extent.YMax
            });
        }
    }

2、调用代码(MainPage.xaml.cs)

int KWID = 4326;
             Envelope extent = new Envelope(105.487843, 31.706997, 111.239656, 39.58577);
            extent.SpatialReference = new SpatialReference(KWID);
            string Url = "http://localhost:8080/geoserver/wms";

string[] layers = new string[] { "shanxi:piovrnce_region", "shanxi:city_region", "shanxi:county_region", "shanxi:capital_point", "shanxi:city_point", "shanxi:county_point" };
            WMSMapServiceLayer wms = new WMSMapServiceLayer(extent, KWID, Url,layers);
            this.myMap.Layers.Add(wms);