SharpMap V1.1 For Web教程系列之——地圖展示,sharpmapv1.1
開篇先說本次的開發環境吧。采用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;
layCityLabel.Style.Font = new Font(FontFamily.GenericSerif, 11);
layCityLabel.MaxVisible = layLabel.MinVisible;
layCityLabel.Style.HorizontalAlignment = LabelStyle.HorizontalAlignmentEnum.Left;
layCityLabel.Style.VerticalAlignment = LabelStyle.VerticalAlignmentEnum.Bottom;
layCityLabel.Style.Offset = new PointF(3, 3);
layCityLabel.Style.Halo = new Pen(Color.Yellow, 2);
layCityLabel.TextRenderingHint = TextRenderingHint.AntiAlias;
layCityLabel.SmoothingMode = SmoothingMode.AntiAlias;
layCityLabel.SRID = 4326;
layCityLabel.LabelFilter = LabelCollisionDetection.ThoroughCollisionDetection;
layCityLabel.Style.CollisionDetection = true;
//Add the layers to the map object.
//The order we add them in are the order they are drawn, so we add the rivers last to put them on top
map.Layers.Add(layCountries);
map.Layers.Add(layRivers);
map.Layers.Add(layCities);
map.Layers.Add(layLabel);
map.Layers.Add(layCityLabel);
//limit the zoom to 360 degrees width
map.MaximumZoom = 360;
map.BackColor = Color.LightBlue;
map.Zoom = 360;
map.Center = new Point(0, 0);
HttpContext.Current.Trace.Write("Map initialized");
return map;
}
public static Map InitializeTaiyuanMap(Size size)
{
HttpContext.Current.Trace.Write("Initializing map...");
//Initialize a new map of size 'imagesize'
SharpMap.Map map = new SharpMap.Map(size);
//設置太原市區域圖層
SharpMap.Layers.VectorLayer layTy = new SharpMap.Layers.VectorLayer("ty");
layTy.DataSource = new SharpMap.Data.Providers.ShapeFile(HttpContext.Current.Server.MapPath(@"~\App_data\Taiyuan\區縣級行政區劃R.shp"), true);
layTy.Style.Fill = new SolidBrush(Color.FromArgb(242, 239, 233));
layTy.Style.Outline = System.Drawing.Pens.Black;
layTy.Style.EnableOutline = true;
layTy.SRID = 4326;
//設置鎮的圖層
SharpMap.Layers.VectorLayer layZ = new SharpMap.Layers.VectorLayer("z");
layZ.DataSource = new SharpMap.Data.Providers.ShapeFile(HttpContext.Current.Server.MapPath(@"~\App_data\Taiyuan\鎮.shp"), true);
layZ.Style.Fill = new SolidBrush(Color.FromArgb(191, 237, 245));
layZ.Style.Outline = System.Drawing.Pens.Black;
layZ.Style.EnableOutline = true;
layZ.SRID = 4326;
//設置河流的圖層
SharpMap.Layers.VectorLayer layHl = new SharpMap.Layers.VectorLayer("Hl");
layHl.DataSource = new SharpMap.Data.Providers.ShapeFile(HttpContext.Current.Server.MapPath(@"~\App_data\Taiyuan\河流湖泊R.shp"), true);
layHl.Style.Fill = new SolidBrush(Color.FromArgb(151, 219, 242));
layHl.Style.Outline = System.Drawing.Pens.Black;
layHl.Style.EnableOutline = true;
layHl.SRID = 4326;
//設置國道的圖層
SharpMap.Layers.VectorLayer layGd = new SharpMap.Layers.VectorLayer("gd");
layGd.DataSource = new SharpMap.Data.Providers.ShapeFile(HttpContext.Current.Server.MapPath(@"~\App_data\Taiyuan\國道L.shp"), true);
layGd.Style.Fill = new SolidBrush(Color.FromArgb(255, 254, 169));
layZ.Style.Outline = System.Drawing.Pens.Black;
layZ.Style.EnableOutline = true;
layGd.Style.Line = new Pen(Color.FromArgb(130, 130, 130), 1);
layGd.SRID = 4326;
//Set up the countries layer
SharpMap.Layers.VectorLayer laySd = new SharpMap.Layers.VectorLayer("sd");
//Set the datasource to a shapefile in the App_data folder
laySd.DataSource = new SharpMap.Data.Providers.ShapeFile(HttpContext.Current.Server.MapPath(@"~\App_data\Taiyuan\省道L.shp"), true);
//Set fill-style to green
laySd.Style.Fill = new SolidBrush(Color.FromArgb(255, 254, 169));
//Set the polygons to have a black outline
laySd.Style.Line = new Pen(Color.FromArgb(130, 130, 130), 1);
layZ.Style.Outline = System.Drawing.Pens.Gainsboro;
layZ.Style.EnableOutline = true;
laySd.SRID = 4326;
//Set up a river layer
SharpMap.Layers.VectorLayer layRivers = new SharpMap.Layers.VectorLayer("Rivers");
//Set the datasource to a shapefile in the App_data folder
layRivers.DataSource = new SharpMap.Data.Providers.ShapeFile(HttpContext.Current.Server.MapPath(@"~\App_data\Taiyuan\城市主干道L.shp"), true);
layRivers.Style.Fill = new SolidBrush(Color.FromArgb(255, 254, 169));
//Define a blue 1px wide pen
layRivers.Style.Line = new Pen(Color.FromArgb(130, 130, 130), 1);
layRivers.SRID = 4326;
//Set up a river layer
SharpMap.Layers.VectorLayer layCities = new SharpMap.Layers.VectorLayer("Cities");
//Set the datasource to a shapefile in the App_data folder
layCities.DataSource = new SharpMap.Data.Providers.ShapeFile(HttpContext.Current.Server.MapPath(@"~\App_data\Taiyuan\城市次干道L.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 = 0.2;
layCities.SRID = 4326;
//Set up a river layer
SharpMap.Layers.VectorLayer layDb = new SharpMap.Layers.VectorLayer("db");
//Set the datasource to a shapefile in the App_data folder
layDb.DataSource = new SharpMap.Data.Providers.ShapeFile(HttpContext.Current.Server.MapPath(@"~\App_data\Taiyuan\基礎地標.shp"), true);
//Define a blue 1px wide pen
//layCities.Style.Symbol = new Bitmap(HttpContext.Current.Server.MapPath(@"~\App_data\Taiyuan\icon.png"));
layDb.Style.SymbolScale = 0.8f;
layDb.MaxVisible = 0.05;
layDb.SRID = 4326;
//Set up a 鎮 label layer
SharpMap.Layers.LabelLayer layZLabel = new SharpMap.Layers.LabelLayer("tyz labels");
layZLabel.DataSource = layZ.DataSource;
layZLabel.Enabled = true;
layZLabel.LabelColumn = "Name";
layZLabel.Style = new SharpMap.Styles.LabelStyle();
layZLabel.Style.ForeColor = Color.White;
layZLabel.Style.Font = new Font(FontFamily.GenericSerif, 12);
layZLabel.Style.BackColor = new System.Drawing.SolidBrush(Color.Black);
layZLabel.MaxVisible = 0.1;
layZLabel.MinVisible = 0.05;
layZLabel.Style.HorizontalAlignment = SharpMap.Styles.LabelStyle.HorizontalAlignmentEnum.Center;
layZLabel.SRID = 4326;
layZLabel.MultipartGeometryBehaviour = SharpMap.Layers.LabelLayer.MultipartGeometryBehaviourEnum.Largest;
//Set up a city label layer
SharpMap.Layers.LabelLayer layCityLabel = new SharpMap.Layers.LabelLayer("City labels");
layCityLabel.DataSource = layCities.DataSource;
layCityLabel.Enabled = true;
layCityLabel.LabelColumn = "Name";
layCityLabel.Style = new SharpMap.Styles.LabelStyle();
layCityLabel.Style.ForeColor = Color.Black;
layCityLabel.Style.Font = new Font(FontFamily.GenericSerif, 11);
layCityLabel.MaxVisible = layZLabel.MinVisible;
layCityLabel.Style.HorizontalAlignment = SharpMap.Styles.LabelStyle.HorizontalAlignmentEnum.Left;
layCityLabel.Style.VerticalAlignment = SharpMap.Styles.LabelStyle.VerticalAlignmentEnum.Bottom;
layCityLabel.Style.Offset = new PointF(3, 3);
layCityLabel.Style.Halo = new Pen(Color.Yellow, 2);
layCityLabel.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
layCityLabel.SmoothingMode = SmoothingMode.AntiAlias;
layCityLabel.SRID = 4326;
layCityLabel.LabelFilter = SharpMap.Rendering.LabelCollisionDetection.ThoroughCollisionDetection;
layCityLabel.Style.CollisionDetection = true;
//Set up a city label layer
SharpMap.Layers.LabelLayer layDbLabel = new SharpMap.Layers.LabelLayer("Db labels");
layDbLabel.DataSource = layDb.DataSource;
layDbLabel.Enabled = true;
layDbLabel.LabelColumn = "Name";
layDbLabel.Style = new SharpMap.Styles.LabelStyle();
layDbLabel.Style.ForeColor = Color.Black;
layDbLabel.Style.Font = new Font(FontFamily.GenericSerif, 11);
layDbLabel.MaxVisible = layCityLabel.MinVisible;
layDbLabel.Style.HorizontalAlignment = SharpMap.Styles.LabelStyle.HorizontalAlignmentEnum.Left;
layDbLabel.Style.VerticalAlignment = SharpMap.Styles.LabelStyle.VerticalAlignmentEnum.Bottom;
layDbLabel.Style.Offset = new PointF(3, 3);
layDbLabel.Style.Halo = new Pen(Color.Yellow, 2);
layDbLabel.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
layDbLabel.SmoothingMode = SmoothingMode.AntiAlias;
layDbLabel.SRID = 4326;
layDbLabel.LabelFilter = SharpMap.Rendering.LabelCollisionDetection.ThoroughCollisionDetection;
layCityLabel.Style.CollisionDetection = true;
//Add the layers to the map object.
//The order we add them in are the order they are drawn, so we add the rivers last to put them on top
map.Layers.Add(layTy);
map.Layers.Add(layHl);
map.Layers.Add(layGd);
map.Layers.Add(laySd);
map.Layers.Add(layRivers);
map.Layers.Add(layCities);
map.Layers.Add(layDb);
map.Layers.Add(layZLabel);
map.Layers.Add(layCityLabel);
map.Layers.Add(layDbLabel);
//limit the zoom to 360 degrees width
map.MaximumZoom = 4;
map.BackColor = Color.White;
map.Zoom = 4;
//map.Center = new SharpMap.Geometries.Point(0, 0);
map.Center = new Point(112.48, 37.86);
HttpContext.Current.Trace.Write("Map initialized");
return map;
}
}
View Code