1、启动Visual Studio 2012 并创建一个新的Windows应用程序
2、调整项目到.net Framework 4.0全框架
3、打开Form1的设计视图
4、浏览SharpMap.UI.dll并添加
SharpMap的dll和地图文件网盘共享地址:http://pan.baidu.com/s/1hqzG0de (内含Demo)
5、点击确定如图:
6、拖动MapBox控件插入Form1窗体中
1、添加SharpMap.dll到项目
2、添加地图文件到项目
3、修改窗体构造函数Fomr1()
public Form1() { InitializeComponent(); VectorLayer vlay = new VectorLayer("States") { DataSource = new ShapeFile(@"path_to_data\states_ugl.shp", true) }; mapBox1.Map.Layers.Add(vlay); mapBox1.Map.ZoomToExtents(); mapBox1.Refresh(); mapBox1.ActiveTool = SharpMap.Forms.MapBox.Tools.Pan;//设置平移 }
4、运行地图可以看到地图,并操作放大、缩小、平移
Step3 给图层添加样式
1、修改窗体构造函数Fomr2() 参见Dome
public Form2() { InitializeComponent(); SharpMap.Layers.VectorLayer vlay = new SharpMap.Layers.VectorLayer("States"); vlay.DataSource = new SharpMap.Data.Providers.ShapeFile(@"path_to_data\states_ugl.shp", true); //构造土地样式 VectorStyle landStyle = new VectorStyle(); landStyle.Fill = new SolidBrush(Color.FromArgb(232, 232, 232)); //构造水样式 VectorStyle waterStyle = new VectorStyle(); waterStyle.Fill = new SolidBrush(Color.FromArgb(198, 198, 255)); //创建地图 Dictionary<string, SharpMap.Styles.IStyle> styles = new Dictionary<string, IStyle>(); styles.Add("land", landStyle); styles.Add("water", waterStyle); //分配主题 vlay.Theme = new SharpMap.Rendering.Thematics.UniqueValuesTheme<string>("class", styles, landStyle); mapBox1.Map.Layers.Add(vlay); mapBox1.Map.ZoomToExtents(); mapBox1.Refresh(); mapBox1.ActiveTool = SharpMap.Forms.MapBox.Tools.Pan; }
2、运行程序,如图
1、修改From3构造函数()
调用http://sampleserver1.arcgisonline.com/ArcGIS/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/WMSServer 服务器记载数据。
public Form3() { InitializeComponent(); SharpMap.Layers.VectorLayer vlay = new SharpMap.Layers.VectorLayer("States"); vlay.DataSource = new SharpMap.Data.Providers.ShapeFile(@"path_to_data\states_ugl.shp", true); //构造土地样式 VectorStyle landStyle = new VectorStyle(); landStyle.Fill = new SolidBrush(Color.FromArgb(232, 232, 232)); //构造水样式 VectorStyle waterStyle = new VectorStyle(); waterStyle.Fill = new SolidBrush(Color.FromArgb(198, 198, 255)); //创建地图 Dictionary<string, SharpMap.Styles.IStyle> styles = new Dictionary<string, IStyle>(); styles.Add("land", landStyle); styles.Add("water", waterStyle); //分配主题 vlay.Theme = new SharpMap.Rendering.Thematics.UniqueValuesTheme<string>("class", styles, landStyle); mapBox1.Map.Layers.Add(vlay); mapBox1.Map.ZoomToExtents(); mapBox1.Refresh(); mapBox1.ActiveTool = SharpMap.Forms.MapBox.Tools.Pan; SharpMap.Layers.WmsLayer wmsL =new SharpMap.Layers.WmsLayer("US Cities","http://sampleserver1.arcgisonline.com/ArcGIS/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/WMSServer"); //转换为PNG wmsL.SetImageFormat("image/png"); //11.0版本 wmsL.Version = "1.1.0"; //添加城市图层 服务名称2 wmsL.AddLayer("2"); //设置 SRID wmsL.SRID = 4326; mapBox1.Map.Layers.Add(wmsL); }
2、运行程序如图,显示城镇。
在这个步骤中,可以结合网上瓦片服务器数据连同本地数据显示。
1、添加BruTile.dll、ProjNet.dll、GeoAPI.dll 到项目中
2、添加辅助方法来创建google坐标系
private GeoAPI.CoordinateSystems.IProjectedCoordinateSystem GetEPSG900913(ProjNet.CoordinateSystems.CoordinateSystemFactory csFact) { List<GeoAPI.CoordinateSystems.ProjectionParameter> parameters = new List<GeoAPI.CoordinateSystems.ProjectionParameter>(); parameters.Add(new GeoAPI.CoordinateSystems.ProjectionParameter("semi_major", 6378137.0)); parameters.Add(new GeoAPI.CoordinateSystems.ProjectionParameter("semi_minor", 6378137.0)); parameters.Add(new GeoAPI.CoordinateSystems.ProjectionParameter("latitude_of_origin", 0.0)); parameters.Add(new GeoAPI.CoordinateSystems.ProjectionParameter("central_meridian", 0.0)); parameters.Add(new GeoAPI.CoordinateSystems.ProjectionParameter("scale_factor", 1.0)); parameters.Add(new GeoAPI.CoordinateSystems.ProjectionParameter("false_easting", 0.0)); parameters.Add(new GeoAPI.CoordinateSystems.ProjectionParameter("false_northing", 0.0)); GeoAPI.CoordinateSystems.IProjection projection = csFact.CreateProjection("Google Mercator", "mercator_1sp", parameters); GeoAPI.CoordinateSystems.IGeographicCoordinateSystem wgs84 = csFact.CreateGeographicCoordinateSystem( "WGS 84", ProjNet.CoordinateSystems.AngularUnit.Degrees, ProjNet.CoordinateSystems.HorizontalDatum.WGS84, ProjNet.CoordinateSystems.PrimeMeridian.Greenwich, new GeoAPI.CoordinateSystems.AxisInfo("north", GeoAPI.CoordinateSystems.AxisOrientationEnum.North), new GeoAPI.CoordinateSystems.AxisInfo("east", GeoAPI.CoordinateSystems.AxisOrientationEnum.East) ); GeoAPI.CoordinateSystems.IProjectedCoordinateSystem epsg900913 = csFact.CreateProjectedCoordinateSystem("Google Mercator", wgs84, projection, ProjNet.CoordinateSystems.LinearUnit.Metre, new GeoAPI.CoordinateSystems.AxisInfo("East", GeoAPI.CoordinateSystems.AxisOrientationEnum.East), new GeoAPI.CoordinateSystems.AxisInfo("North", GeoAPI.CoordinateSystems.AxisOrientationEnum.North)); return epsg900913; }
3、修改构造函数Form4()
public Form4() { InitializeComponent(); SharpMap.Layers.VectorLayer vlay = new SharpMap.Layers.VectorLayer("States"); vlay.DataSource = new SharpMap.Data.Providers.ShapeFile(@"path_to_data\states_ugl.shp", true); //构造土地样式 VectorStyle landStyle = new VectorStyle(); landStyle.Fill = new SolidBrush(Color.FromArgb(232, 232, 232)); //创造水样式 VectorStyle waterStyle = new VectorStyle(); waterStyle.Fill = new SolidBrush(Color.FromArgb(198, 198, 255)); //创造地图 Dictionary<string, SharpMap.Styles.IStyle> styles = new Dictionary<string, IStyle>(); styles.Add("land", landStyle); styles.Add("water", waterStyle); //分配主题 vlay.Theme = new SharpMap.Rendering.Thematics.UniqueValuesTheme<string>("class", styles, landStyle); mapBox1.Map.Layers.Add(vlay); ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory ctFact = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory(); ProjNet.CoordinateSystems.CoordinateSystemFactory csFact = new ProjNet.CoordinateSystems.CoordinateSystemFactory(); vlay.CoordinateTransformation = ctFact.CreateFromCoordinateSystems(ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84, GetEPSG900913(csFact)); vlay.ReverseCoordinateTransformation = ctFact.CreateFromCoordinateSystems(GetEPSG900913(csFact), ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84); mapBox1.Map.BackgroundLayer.Add(new SharpMap.Layers.TileAsyncLayer( new BruTile.Web.OsmTileSource(), "OSM")); mapBox1.Map.ZoomToExtents(); mapBox1.Refresh(); mapBox1.ActiveTool = SharpMap.Forms.MapBox.Tools.Pan; }
4、运行程序,如图: