Click here to Skip to main content
13,354,132 members (56,255 online)
Click here to Skip to main content
Articles » Web Development » ASP.NET » Samples » Downloads


108 bookmarked
Posted 24 Nov 2005

Creating an Interactive Map in ASP.NET 2.0 Using SharpMap

, 25 Nov 2005
An article on how to create a web map application using SharpMap in ASP.NET 2.0.
using System;
using System.Data;
using System.Drawing;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Default : System.Web.UI.Page
	private SharpMap.Map myMap;

	protected void Page_Load(object sender, EventArgs e)
		//Set up the map
		myMap = InitializeMap(new System.Drawing.Size((int)imgMap.Width.Value,(int)imgMap.Height.Value));
		if (Page.IsPostBack) 
			//Page is post back. Restore center and zoom-values from viewstate
			myMap.Center = (SharpMap.Geometries.Point)ViewState["mapCenter"];
			myMap.Zoom = (double)ViewState["mapZoom"];
			//This is the initial view of the map. Zoom to the extents of the map:
			//Save the current mapcenter and zoom in the viewstate
			ViewState.Add("mapCenter", myMap.Center);
			ViewState.Add("mapZoom", myMap.Zoom);
			//Create the map
	protected void imgMap_Click(object sender, ImageClickEventArgs e)
		//Set center of the map to where the client clicked
		myMap.Center = SharpMap.Utilities.Transform.MapToWorld(new System.Drawing.Point(e.X, e.Y), myMap);
		//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;
		//Save the new map's zoom and center in the viewstate
		ViewState.Add("mapCenter", myMap.Center);
		ViewState.Add("mapZoom", myMap.Zoom);
		//Create the map
	/// <summary>
	/// Sets up the map, add layers and sets styles
	/// </summary>
	/// <param name="outputsize">Initiatial size of output image</param>
	/// <returns>Map object</returns>
	private SharpMap.Map InitializeMap(System.Drawing.Size outputsize)
		//Initialize a new map of size 'imagesize'
		SharpMap.Map map = new SharpMap.Map(outputsize);
		//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.Providers.ShapeFile(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 = System.Drawing.Pens.Black;
		layCountries.Style.EnableOutline = true;

		//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.Providers.ShapeFile(Server.MapPath(@"~\App_data\rivers.shp"), true);
		//Define a blue 1px wide pen
		layRivers.Style.Line = new Pen(Color.Blue,1);

		//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
		return map;

	/// <summary>
	/// Creates the map, inserts it into the cache and sets the ImageButton Url
	/// </summary>
	private void CreateMap()
		System.Drawing.Image img = myMap.GetMap();
		string imgID = SharpMap.Web.Caching.InsertIntoCache(1, img);
		imgMap.ImageUrl = "getmap.aspx?ID=" + HttpUtility.UrlEncode(imgID);

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Morten on GIS
Web Developer
United States United States
Morten Nielsen holds a M.Sc. in Engineering and GeoInformatics, and works full time as a .NET software developer in California making the next generation GIS applications. He specializes in mapping, GeoInformatics and image processing, and is the creator of several open source projects such as Proj.NET, SharpGPS and SharpMap.

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 25 Nov 2005
Article Copyright 2005 by Morten on GIS
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid