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

How to Zoom to Selected feature?

Topics: Algorithms
May 18, 2012 at 4:09 AM

Hi All,

I need to work on search, for example if I typed HCM in search textbox and press enter, it shold zoom to the HCM location on map (datasource is postgis). Help me!!!


May 31, 2012 at 7:53 AM


you can find HCM and get bindingbox then use map.zoomtobox(bindingbox)

Jun 18, 2012 at 4:04 AM

Here my code:


private void ZoomToSelectedFeature(SharpMap.Map map, object value)
            SharpMap.Data.Providers.PostGIS objPostGIS_Data = new SharpMap.Data.Providers.PostGIS(PostGisConnectionString, "tbl_City", "geo", "geo_id");
            objPostGIS_Cot.DefinitionQuery = "geo_id = '" + value + "'";

            //Get providers extents
            var bb = objPostGIS_Cot.GetExtents();            

            //Perform Query
            SharpMap.Data.FeatureDataSet fds = new SharpMap.Data.FeatureDataSet();
            objPostGIS_Cot.ExecuteIntersectionQuery(bb, fds);

            //Remove old selected layer, if any
            //Todo: Store style settings
            SharpMap.Layers.VectorLayer vlSelected = map.Layers[DisplayMap.QueryLayerIndex] as SharpMap.Layers.VectorLayer;
            if (vlSelected != null && vlSelected.LayerName == "_SELECTED_")

            //Did the query return any results
            if (fds.Tables.Count > 0)
                SharpMap.Data.FeatureDataTable fdt = fds.Tables[0];
                if (fdt.Rows.Count > 0)
                    //Set up new selected layer
                    SharpMap.Data.Providers.GeometryFeatureProvider gfp =
                        new SharpMap.Data.Providers.GeometryFeatureProvider(fdt);
                    map.Layers.Add(new SharpMap.Layers.VectorLayer("_SELECTED_", gfp));

I find the bb's but cannot zoom to the object is found on the map. Why?


Jun 18, 2012 at 12:42 PM

Maybe all you need to do is to refresh the visual (MapBox)

Jun 19, 2012 at 3:04 AM

nice, thanks FObermaier