开篇先说本次的开发环境吧。采用Vs2010,.Net Framework 4.0。
为了更好的调试程序,建议在IIS中进行调试及运行,个人非常不喜欢利用VS自己提供的WebServer去调试程序,而且很多在Web.config中的设置也需要在IIS中才能起到效果!
开发环境我就不要介绍了,先来说说SharpMap的组件要求吧。由于SharpMap的架构一直在变化和改进过程中,因此参考网络上别人的事例代码,你会发现都运行不起来,不是接口没了,就是命名空间变了,这点我也希望SharpMap早日稳定下来。
这次使用的SharpMap的版本是V1.1版本,官方意见提供最新稳定版的下载了,官方网址为:http://sharpmap.codeplex.com/
SharpMap 1.1版本的下载地址为:http://sharpmap.codeplex.com/downloads/get/792797,发布时间为2014年12月11日;该版本只是SharpMap的核心库(Core+UI),下载完后,为了Web开发还必须下载一个Web端的库,本人做完因为这一步走了好多弯路,网络上的教程也没有人写上着一点。在官网的DOWNLOADS节点下有个下载界面,需要下载SharpMap.Web这个组件。
OK!所需库完成后,下面进行asp.net的网站开发!你也可以不看下面的代码,直接下载整个网站。解决方案下载地址:http://pan.baidu.com/s/1i3vdUcd
打开VS2010,新建一个网站,新建一个WebForm,我这里命名为“Map.aspx”,下面贴代码:
Map.aspx:地图展示页面
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Map.aspx.cs" Inherits="Map" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title>SharpMap测试</title></head><body> <form id="form1" runat="server"> <div> <asp:RadioButtonList ID="rblMapTools" runat="server" RepeatDirection="Horizontal"> <asp:ListItem Value="0">Zoom in</asp:ListItem> <asp:ListItem Value="1">Zoom out</asp:ListItem> <asp:ListItem Value="2" Selected="True">Pan</asp:ListItem> </asp:RadioButtonList> <asp:ImageButton runat="server" Width="700" Height="400" ID="imgMap" onclick="imgMap_Click" /> </div> </form></body></html>View Code
Map.aspx.cx:
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;public partial class Map : System.Web.UI.Page{ PRivate SharpMap.Map myMap; protected void Page_Load(object sender, EventArgs e) { myMap = MapHelper.InitializeMap(new System.Drawing.Size((int)imgMap.Width.Value, (int)imgMap.Height.Value)); //SharpMap.Map if (this.IsPostBack) { myMap.Center = (GeoAPI.Geometries.Coordinate)ViewState["mapCenter"]; myMap.Zoom = (double)ViewState["mapZoom"]; } else { this.generateMap(); } } protected void imgMap_Click(object sender, ImageClickEventArgs e) { myMap.Center = myMap.ImageToWorld(new System.Drawing.Point(e.X, e.Y)); //Set zoom value if any of the zoom tools were selected if (rblMapTools.SelectedValue == "0") //Zoom in myMap.Zoom = myMap.Zoom * 0.5; else if (rblMapTools.SelectedValue == "1") //Zoom out myMap.Zoom = myMap.Zoom * 2; //Create the map this.generateMap(); } private void generateMap() { ViewState.Add("mapCenter", myMap.Center); ViewState.Add("mapZoom", myMap.Zoom); //myMap = MapHelper.InitializeMap(new System.Drawing.Size(256, 256)); System.Drawing.Image img = myMap.GetMap(); string imgID = SharpMap.Web.Caching.InsertIntoCache(1, img); imgMap.ImageUrl = "getmap.aspx?ID=" + HttpUtility.UrlEncode(imgID); }}
Web.Config配置文件,在.Net 4.0下配置文件,红色部分表示这个地方和SharpMap官网以及互联网上很多教程里面的区别。
<?xml version="1.0"?><configuration> <system.web> <compilation debug="true" targetFramework="4.0" /> </system.web> <system.webServer> <modules runAllManagedModulesForAllRequests="true"/> <handlers> <add verb="*" name="test" path="GetMap.aspx" type="SharpMap.Web.HttpHandler" preCondition="integratedMode"/> </handlers> <validation validateIntegratedModeConfiguration="false" /> </system.webServer></configuration>
MapHelper.cs
using System;using System.Collections.Generic;using System.Linq;using System.Web;using SharpMap;using System.Drawing;using SharpMap.Layers;using SharpMap.Data.Providers;using SharpMap.Styles;using System.Drawing.Text;using SharpMap.Rendering;using ColorBlend=SharpMap.Rendering.Thematics.ColorBlend;using Point=GeoAPI.Geometries.Coordinate;using System.Drawing.Drawing2D;/// <summary>/// Summary description for MapHelper/// </summary>public class MapHelper{ public MapHelper() { } public static Map InitializeMap(Size size) { HttpContext.Current.Trace.Write("Initializing map..."); //Initialize a new map of size 'imagesize' Map map = new Map(size); //Set up the countries layer VectorLayer layCountries = new VectorLayer("Countries"); //Set the datasource to a shapefile in the App_data folder layCountries.DataSource = new ShapeFile(HttpContext.Current.Server.MapPath(@"~/App_data/countries.shp"), true); //Set fill-style to green layCountries.Style.Fill = new SolidBrush(Color.Green); //Set the polygons to have a black outline layCountries.Style.Outline = Pens.Black; layCountries.Style.EnableOutline = true; layCountries.SRID = 4326; //Set up a river layer VectorLayer layRivers = new VectorLayer("Rivers"); //Set the datasource to a shapefile in the App_data folder layRivers.DataSource = new ShapeFile(HttpContext.Current.Server.MapPath(@"~/App_data/rivers.shp"), true); //Define a blue 1px wide pen layRivers.Style.Line = new Pen(Color.Blue, 1); layRivers.SRID = 4326; //Set up a river layer VectorLayer layCities = new VectorLayer("Cities"); //Set the datasource to a shapefile in the App_data folder layCities.DataSource = new ShapeFile(HttpContext.Current.Server.MapPath(@"~/App_data/cities.shp"), true); //Define a blue 1px wide pen //layCities.Style.Symbol = new Bitmap(HttpContext.Current.Server.MapPath(@"~/App_data/Taiyuan/icon.png")); layCities.Style.SymbolScale = 0.8f; layCities.MaxVisible = 40; layCities.SRID = 4326; //Set up a country label layer LabelLayer layLabel = new LabelLayer("Country labels"); layLabel.DataSource = layCountries.DataSource; layLabel.Enabled = true; layLabel.LabelColumn = "Name"; layLabel.Style = new LabelStyle(); layLabel.Style.ForeColor = Color.White; layLabel.Style.Font = new Font(FontFamily.GenericSerif, 12); layLabel.Style.BackColor = new SolidBrush(Color.FromArgb(128, 255, 0, 0)); layLabel.MaxVisible = 90; layLabel.MinVisible = 30; layLabel.Style.HorizontalAlignment = LabelStyle.HorizontalAlignmentEnum.Center; layLabel.SRID = 4326; layLabel.MultipartGeometryBehaviour = LabelLayer.MultipartGeometryBehaviourEnum.Largest; //Set up a city label layer LabelLayer layCityLabel = new LabelLayer("City labels"); layCityLabel.DataSource = layCities.DataSource; layCityLabel.Enabled = true; layCityLabel.LabelColumn = "Name"; layCityLabel.Style = new LabelStyle(); layCityLabel.Style.ForeColor = Color.Black;
新闻热点
疑难解答