用arcgis的silverlight API连接Geoserver
本人是silverlight和Arcgis开发的新手,最近因为项目的需要涉及到silverlight和Arcgis的相关开发。写这篇博客的目的一方面是为了总结经验,加深记忆,另一方也是为了能够和网上的高手们多交流。希望看这篇文章的朋友能够不吝赐教。谢谢!
因为项目的成本问题,加上项目本身对功能要求并不是很高,所以我们只能用开源免费的Geoserver作为后台地图服务器。在此要感谢以下几篇博文给我的帮助。
http://www.cnblogs.com/beniao/archive/2011/01/10/1930995.html
http://www.cnblogs.com/wenjl520/archive/2009/06/02/1494149.html
写的非常好。感谢博主!
首先,贴上我的代码:
/// <summary>
/// 用arcgis silverlight api 连接Geoserver服务器
/// </summary>
public class ArcGisGeoWMSLayer : DynamicMapServiceLayer
{
public static DependencyProperty URLProperty = DependencyProperty.Register("URL", typeof(string), typeof(ArcGisGeoWMSLayer),
new PropertyMetadata("http://localhost:8080/wms", null));
/// <summary>
/// GeoServer URI
/// </summary>
public string URL
{
get { return (string)GetValue(URLProperty); }
set { SetValue(URLProperty, value); }
}
public static DependencyProperty LayerNameProperty = DependencyProperty.Register("Layer", typeof(string), typeof(ArcGisGeoWMSLayer),
new PropertyMetadata(string.Empty, null));
/// <summary>
/// Layer name
/// </summary>
public string Layer
{
get { return (string)GetValue(LayerNameProperty); }
set
{
SetValue(LayerNameProperty, value);
}
}
public static DependencyProperty ExtentProperty = DependencyProperty.Register("Extent", typeof(Envelope), typeof(ArcGisGeoWMSLayer),
new PropertyMetadata(((object)new Envelope(0, 0, 0, 0)), OnExtentPropertyChanged));
private static void OnExtentPropertyChanged(DependencyObject dp, DependencyPropertyChangedEventArgs e)
{
}
/// <summary>
/// 地图边界范围(从geoserver发布地图时可以获取)
/// </summary>
public Envelope Extent
{
get { return (Envelope)GetValue(ExtentProperty); }
set
{
SetValue(ExtentProperty, value);
this.FullExtent = Extent;
}
}
public override void GetUrl(Envelope extent, int width, int height, OnUrlComplete onComplete)
{
int extentWKID = extent.SpatialReference.WKID;
StringBuilder mapURL = new StringBuilder();
mapURL.Append(URL);
mapURL.Append("?service=WMS");
mapURL.AppendFormat("&version={0}", "1.1.0");
mapURL.Append("&request=GetMap");
mapURL.AppendFormat("&layers={0}", Layer);
mapURL.Append("&styles=");
mapURL.AppendFormat("&bbox={0},{1},{2},{3}", extent.XMin.ToString(), extent.YMin.ToString(), extent.XMax.ToString(), extent.YMax.ToString());
mapURL.AppendFormat("&width={0}", width);
mapURL.AppendFormat("&height={0}", height);
mapURL.AppendFormat("&format={0}", "image/png");
onComplete(mapURL.ToString(), width, height, new ESRI.ArcGIS.Client.Geometry.Envelope()
{
XMin = extent.XMin,
YMin = extent.YMin,
XMax = extent.XMax,
YMax = extent.YMax
});
}
}
其实原理很简单,geoserver 发布的是未经缓存的地图服务,因此需要从DynamicMapServiceLayer继承,重写GetUrl即可。需要注意的是,在初始化类的时候,需要将地图的extent赋给FullExtent属性,否则地图无法正常显示:
前台XAML代码:
<UserControl x:Class="ArcGisSlGeoDemo.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:esri="http://schemas.esri.com/arcgis/client/2009"
xmlns:customLayer="clr-namespace:ArcGisSlGeoDemo"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
<esri:Map x:Name="baseMap" IsLogoVisible="False">
<customLayer:ArcGisGeoWMSLayer URL="http://localhost:8080/geoserver/wms" Layer="JianxiYP:JiangxiYP" ID="geoLayer"></customLayer:ArcGisGeoWMSLayer>
</esri:Map>
</Grid>
</UserControl>
注意要给Extent赋值,这个是地图边界范围,在geoserver发布地图的时候我从geoserver里面直接读取出来的边界范围。
MainPage.cs
public MainPage()
{
InitializeComponent();
Envelope extent = new Envelope(73580, 20411.591, 75418.814, 21975.971);
extent.SpatialReference = new SpatialReference(4326);
ArcGisGeoWMSLayer layer = this.baseMap.Layers[0] as ArcGisGeoWMSLayer;
layer.Extent = extent;
}
转载自:https://blog.csdn.net/jxzz016590/article/details/8424651