开篇先说本次的开发环境吧。采用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="">Zoom in</asp:ListItem>
<asp:ListItem Value="">Zoom out</asp:ListItem>
<asp:ListItem Value="" Selected="True">Pan</asp:ListItem>
</asp:RadioButtonList>
<asp:ImageButton runat="server" Width="" Height="" ID="imgMap"
onclick="imgMap_Click" />
</div>
</form>
</body>
</html>

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 = ; //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, );
layRivers.SRID = ; //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 = ;
layCities.SRID = ; //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, );
layLabel.Style.BackColor = new SolidBrush(Color.FromArgb(, , , ));
layLabel.MaxVisible = ;
layLabel.MinVisible = ;
layLabel.Style.HorizontalAlignment = LabelStyle.HorizontalAlignmentEnum.Center;
layLabel.SRID = ;
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, );
layCityLabel.MaxVisible = layLabel.MinVisible;
layCityLabel.Style.HorizontalAlignment = LabelStyle.HorizontalAlignmentEnum.Left;
layCityLabel.Style.VerticalAlignment = LabelStyle.VerticalAlignmentEnum.Bottom;
layCityLabel.Style.Offset = new PointF(, );
layCityLabel.Style.Halo = new Pen(Color.Yellow, );
layCityLabel.TextRenderingHint = TextRenderingHint.AntiAlias;
layCityLabel.SmoothingMode = SmoothingMode.AntiAlias;
layCityLabel.SRID = ;
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 = ;
map.BackColor = Color.LightBlue; map.Zoom = ;
map.Center = new Point(, ); 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(, , ));
layTy.Style.Outline = System.Drawing.Pens.Black;
layTy.Style.EnableOutline = true;
layTy.SRID = ; //设置镇的图层
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(, , ));
layZ.Style.Outline = System.Drawing.Pens.Black;
layZ.Style.EnableOutline = true;
layZ.SRID = ; //设置河流的图层
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(, , ));
layHl.Style.Outline = System.Drawing.Pens.Black;
layHl.Style.EnableOutline = true;
layHl.SRID = ; //设置国道的图层
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(, , ));
layZ.Style.Outline = System.Drawing.Pens.Black;
layZ.Style.EnableOutline = true;
layGd.Style.Line = new Pen(Color.FromArgb(, , ), );
layGd.SRID = ; //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(, , ));
//Set the polygons to have a black outline
laySd.Style.Line = new Pen(Color.FromArgb(, , ), ); layZ.Style.Outline = System.Drawing.Pens.Gainsboro;
layZ.Style.EnableOutline = true; laySd.SRID = ; //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(, , ));
//Define a blue 1px wide pen
layRivers.Style.Line = new Pen(Color.FromArgb(, , ), );
layRivers.SRID = ; //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 = ; //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 = ; //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, );
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 = ;
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, );
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(, );
layCityLabel.Style.Halo = new Pen(Color.Yellow, );
layCityLabel.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
layCityLabel.SmoothingMode = SmoothingMode.AntiAlias;
layCityLabel.SRID = ;
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, );
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(, );
layDbLabel.Style.Halo = new Pen(Color.Yellow, );
layDbLabel.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
layDbLabel.SmoothingMode = SmoothingMode.AntiAlias;
layDbLabel.SRID = ;
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 = ;
map.BackColor = Color.White; map.Zoom = ;
//map.Center = new SharpMap.Geometries.Point(0, 0);
map.Center = new Point(112.48, 37.86); HttpContext.Current.Trace.Write("Map initialized");
return map;
}
}

SharpMap V1.1 For Web教程系列之——地图展示的更多相关文章

  1. SharpMap V1.1 For Web教程系列之——前言

    上次使用SharpMap还是在0.9版本阶段,那个时候主要是为了将SharpMap移植到Windows Mobile环境中,具体可参见原先的文章.互联网真的是风云变幻啊,才短短几年,Windows M ...

  2. openlayers4 入门开发系列之地图展示篇(附源码下载)

    前言 openlayers4 官网的 api 文档介绍地址 openlayers4 api,里面详细的介绍 openlayers4 各个类的介绍,还有就是在线例子:openlayers4 官网在线例子 ...

  3. ASP.NET Web API系列教程目录

    ASP.NET Web API系列教程目录 Introduction:What's This New Web API?引子:新的Web API是什么? Chapter 1: Getting Start ...

  4. ASP.NET Web API系列教程(目录)(转)

    注:微软随ASP.NET MVC 4一起还发布了一个框架,叫做ASP.NET Web API.这是一个用来在.NET平台上建立HTTP服务的Web API框架,是微软的又一项令人振奋的技术.目前,国内 ...

  5. Web攻防系列教程之文件上传攻防解析(转载)

    Web攻防系列教程之文件上传攻防解析: 文件上传是WEB应用很常见的一种功能,本身是一项正常的业务需求,不存在什么问题.但如果在上传时没有对文件进行正确处理,则很可能会发生安全问题.本文将对文件上传的 ...

  6. [转]ASP.NET Web API系列教程(目录)

    本文转自:http://www.cnblogs.com/r01cn/archive/2012/11/11/2765432.html 注:微软随ASP.NET MVC 4一起还发布了一个框架,叫做ASP ...

  7. 【原创分享·微信支付】C# MVC 微信支付教程系列之现金红包

            微信支付教程系列之现金红包           最近最弄这个微信支付的功能,然后扫码.公众号支付,这些都做了,闲着无聊,就看了看微信支付的其他功能,发现还有一个叫“现金红包”的玩意,想 ...

  8. 【原创分享·微信支付】 C# MVC 微信支付教程系列之扫码支付

    微信支付教程系列之扫码支付                  今天,我们来一起探讨一下这个微信扫码支付.何为扫码支付呢?这里面,扫的码就是二维码了,就是我们经常扫一扫的那种二维码图片,例如,我们自己添 ...

  9. 【原创分享·微信支付】 C# MVC 微信支付教程系列之公众号支付

    微信支付教程系列之公众号支付         今天,我们接着讲微信支付的系列教程,前面,我们讲了这个微信红包和扫码支付.现在,我们讲讲这个公众号支付.公众号支付的应用环境常见的用户通过公众号,然后再通 ...

随机推荐

  1. Spark学习之第一个程序 WordCount

    WordCount程序 求下列文件中使用空格分割之后,单词出现的个数 input.txt java scala python hello world java pyfysf upuptop wintp ...

  2. SCADA开源项目lite版本

    一.引子 自从看了老坏猫(江湖人称猫总)的SharpSCADA项目后,让我感觉耳目一新同时也对自动化数据采集有了更深入的认识,我相信有不少做上位机的朋友和我一样对这个项目非常好奇.我们做上位机的应用场 ...

  3. 面试中的 ThreadLocal 原理和使用场景

    相信大家不管是在网上做题还是在面试中都经常被问过 ThreadLocal 的原理和用法,虽然一直知道这个东西的存在但是一直没有好好的研究一下原理,没有自己的知识体系.今天花点时间好好学习了一下,分享给 ...

  4. java 第二章

    变量:变量就是代表程序运行时存放数据的地方 数据存放在:磁盘,内存卡,U盘,光盘,内存条,固态硬盘,机械硬盘等 字节:8个二进制位构成1个"字节(Byte)",它是存储空间的基本计 ...

  5. 最火的分布式 HTAP 数据库 TiDB - 入门实践教程

    偶然在某篇博客看到了 TiDB,一个融合 OLTP 和 OLAP 的分布式开源数据库, GitHub 上 Star 很多,然后 watch 了,发现 commit 和 pull request 一直都 ...

  6. PHP编码风格规范

    由于PHP的灵活性,很多人写起代码来也不讲求一个好的代码规范,使得本就灵活的PHP代码看起来很乱,其实PSR规范中的PSR-1和PSR-2已经定义了在PHP编码中的一些规范,只要我们好好遵守这些规范, ...

  7. Ubuntu 系统如何用pycharm开发python—OpenCV

  8. Windows上切换java8和java11

    Windows上安装了java8和java11,时不时要切换,于是思考写行命令解决.思路是修改java_home变量.我的java_home变量是设置在系统级别的. 修改环境变量有2个命令,set和s ...

  9. 自定义SWT控件五之自定义穿梭框

    5.自定义穿梭框 package com.view.control.shuttlebox; import java.util.ArrayList; import java.util.HashMap; ...

  10. 使用jvisualvm.exe工具远程监视tomcat的线程运行状态

    一.简述 在web项目中,常使用tomcat作为web容器.代码编写的时候,由于业务需要,也常会使用线程机制.在系统运行一段时间之后,若出现响应慢或线程之间出现死锁的情况,要查出问题所在,需要使用jd ...