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

How do I return a set of feature attribute data from a click on a map?

Initialize the map, and add layers
SharpMap.Map myMap = new SharpMap.Map(new Size(400,300));
myMap.MinimumZoom = 100; //Minimum zoom allowed
myMap.BackgroundColor = Color.White; //Set background

//Add PostGIS layer:
VectorLayer myLayer = new SharpMap.Layers.VectorLayer("My layer");
string connStr = "Server=;Port=5432;UserId=postgres;Password=password;Database=myGisDb;";
myLayer.DataSource = new SharpMap.Providers.PostGIS(connStr, "myTable", "the_geom", 32632);
myLayer.MaxVisible = 40000;

// Assumes mapImage1 was added to a Windows Form in the WinForm designer
mapImage1.MouseUp += new MouseEventHandler(mapImage1_MouseUp);
mapImage1.Map = myMap;

The mouse up handler of the map control:
private void mapImage1_MouseUp(object sender, MouseEventArgs e)
  var p = mapImage1.Map.ImageToWorld(e.Location);
  FeatureDataSet ds = new FeatureDataSet();
  foreach (var layer in mapImage1.Map.Layers)
    //Test if the layer is queryable?
    var queryLayer = layer as SharpMap.Layers.ICanQueryLayer;
    if (queryLayer != null)
      queryLayer.ExecuteIntersectionQuery(p.GetBoundingBox(), ds)

  // You can now bind ds.Tables[0] to a datagrid, etc.

Last edited Aug 31, 2012 at 12:55 PM by FObermaier, version 4