This project has moved. For the latest updates, please go here.

Creating a clickable map

Please note that you do not have to create a control yourself, there are at least two map controls for the System.Windows.Forms environment:
  • MapBox
  • MapImage (deprecated)
You can find them in the SharpMap.UI project. Their usage is demonstrated in the WinFormSamples project.

System.Windows.Forms

  1. Insert a PictureBox for Windows.Forms.
  2. Set the image to your map-instance. Ex.: imgMap.Image = myMap.GetMap();
  3. Set up a MouseClick-event for your PictureBox.
  4. Create a MouseClick-event handler, similar to the one below.
protected void imgMap_MouseClick(object sender, MouseClickEventArgs e)
{ 
    //Center the map on the click-point
    myMap.Center = myMap.ImageToWorld(new System.Drawing.PointF(e.X, e.Y), myMap);    
    //Zoom in 2x
    myMap.Zoom *= 0.8;
    //Call function that renders the map and returns it to the client
    myImage.Image = myMap.GetMap();
}
There are ready-to-use UserControls (MapImage/MapBox) in the SharpMap.UI project. In addition to zooming functionality they offer tools for querying and panning.

ASP.NET

  1. Insert an <asp:ImageButton> for ASP.NET web forms.
  2. Set the image to your map-instance. Ex.: imgMap.Image = myMap.GetMap();
  3. Set up a click-event for your ImageButton.
  4. Create a click-event handler, similar to the one below.
protected void imgMap_Click(object sender, ImageClickEventArgs e)
{ 
    //Center the map on the click-point
    myMap.Center = myMap.ImageToWorld(new System.Drawing.PointF(e.X, e.Y), myMap);    
    //Zoom in 2x
    myMap.Zoom *= 0.8;
    //Call function that renders the map and returns it to the client
    myImage.Image = myMap.GetMap();
}
Note: You need to store the zoom and center values between requests. This can be done using either ViewState or Session variables. These should be restored on a postback, and stored when they are changed.

Last edited Aug 31, 2012 at 11:45 AM by FObermaier, version 12