1 运行过程
我们通过实例来讲述SharpMap的运行过程和渲染(绘制)机制。首先打开Simple.aspx,可知此页面有一组单选框(3个,分别是放大、缩小和漫游)和一个图像按钮,用于显示地图。它的代码在Simple.aspx.cs中。
打开Simple.aspx.cs,在Page_Load函数中是页面初始化代码。可知地图生成分两步:
1.1 初始化地图
//Set up the map. We use the method in the App_Code folder for initializing the map
myMap = MapHelper.InitializeMap(new
System.Drawing.Size((int)imgMap.Width.Value,(int)imgMap.Height.Value));
我们找到MapHelper.InitializeMap函数,发现地图的初始化分为以下几步:
(1)创建地图,创建图层
//Initialize a new map of size 'imagesize'
SharpMap.Map map = new SharpMap.Map(size);
//Set up the countries layer
SharpMap.Layers.VectorLayer layCountries = new SharpMap.Layers.VectorLayer("Countries");
//Set the datasource to a shapefile in the App_data folder
layCountries.DataSource = new
SharpMap.Data.Providers.ShapeFile(HttpContext.Current.Server.MapPath(@"~/App_data/countries.shp"), true);
(2)基本的图层显示设置
//Set fill-style to green
layCountries.Style.Fill = new SolidBrush(Color.Green);
//Set the polygons to have a black outline
layCountries.Style.Outline = System.Drawing.Pens.Black;
layCountries.Style.EnableOutline = true;
layCountries.SRID = 4326;
(3)加入图层到地图
//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);
(4)地图放缩、背景、中心等设置
//limit the zoom to 360 degrees width
map.MaximumZoom = 360;
map.BackColor = Color.LightBlue;
map.Zoom = 360;
map.Center = new SharpMap.Geometries.Point(0,0);
1.2 绘制并生成地图
我们回到Simple.aspx.cs的Page_Load函数,发现下一步调用GenerateMap();
//This is the initial view of the map. Zoom to the