This project has moved and is read-only. For the latest updates, please go here.

Search on basic of Location

Topics: Algorithms, SharpMap Project, SharpMap v0.9 / v1.x, SharpMap v2.0, WinForms Controls
Jul 12, 2011 at 2:28 PM

Hi Team,

I need to work on search based upon location. for example if I typed Finland in search textbox and press enter, it shold show me the finland location on map.

Please guide me in right direction to do the job.


Thanks in advance.

Jul 13, 2011 at 9:43 AM

You need a datasource that provides the information

LocationName -> BoundingBox/Envelope/MinX,MinY,MaxX,MaxY

This information may come from a VectorLayer Datasource (IProvider) by setting its DefinitionQuery to e.g. "Name" LIKE 'Finland%' or setting up a FilterDelegate function.

On the KeyPress event check for Return/Enter key and lookup the bounding box, and call Map's ZoomToBox( boundingBox ) function

Hth FObermaier

Jul 13, 2011 at 11:45 AM

Hi FObermaier,

Thanks for the reply. I am using Tilelayer, Please let me know in my case its posiible or not?

I am sending My map preview, Please guide in right direction or if you have some code example please let me know.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using SharpMap.Geometries;
using SharpMap.Layers;
using SharpMap;
using Point = SharpMap.Geometries.Point;
using BruTile.Web;
using BruTile.PreDefined;
using BruTile;
using System.Data.OleDb;
using WMSClient;
using SharpMap.Data.Providers;
using ProjNet.CoordinateSystems.Transformations;
using ProjNet.CoordinateSystems;
using ProjNet.Converters.WellKnownText;

namespace WinFormSamples
    public partial class WMSclient : UserControl
        //--> Define the SharpMap object
        SharpMap.Map _globalMap;    
        public SharpMap.Geometries.Point clickPoint;
        public SharpMap.Geometries.Point zoomAroundPoint;
        SharpMap.Layers.LabelLayer xlsLabelLayer = null;
        MapObj[] _globalMapObj = null;
        public SharpMap.Map SharpMapObject
            get { return _globalMap; }
            set { _globalMap = value; }

        //--> Set the zoom factor percentage
        const float ZOOM_FACTOR = 0.3f;
        const float ROTATE_FACTOR = 45f;

        public WMSclient()

            //--> InitializeMap();
            _globalMap = WMSclient.InitializeMap();
            mapImage.Map = _globalMap;
            clickPoint = new Point();
            clickPoint.X = 0.0000000;
            clickPoint.Y = 0.0000000;


        public  static Map InitializeMap()
            var map = new SharpMap.Map(new System.Drawing.Size(700, 700));
            //--> Get background           
            var osmLayer = new TileLayer(new OsmTileSource(), "OSM");
            return map;

        public void AddMarker(MapObj[] list)

            _globalMapObj = list;
            //create Datatable with columns
            var ds = new System.Data.DataSet("XLS");
            var dt = new System.Data.DataTable("MarkerDataTable");
            dt.Columns.Add("OID", typeof(int));
            dt.Columns.Add("X", typeof(double));
            dt.Columns.Add("Y", typeof(double));
            dt.Columns.Add("Name", typeof(string));
            //Add Row to DataTable
            for (int i = 0; i < list.Length; i++)
                dt.Rows.Add(new object[] { i+1,list[i].Longitude, list[i].Latitude,list[i].Name });               

            //Set up provider
            var xlsProvider = new SharpMap.Data.Providers.DataTablePoint(ds.Tables[0], "OID", "X", "Y");
            var xlsLayer = new SharpMap.Layers.VectorLayer("XLS", xlsProvider);
            //Add this line if you want cutom image.
           // xlsLayer.Style.Symbol = new System.Drawing.Bitmap("image/pin_map.png");
            xlsLayer.Style.Symbol = SharpMap.Styles.VectorStyle.DefaultSymbol;          

            //The SRS for this datasource is EPSG:4326, therefore we need to transfrom it to OSM projection
            var ctf = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory();
            var cf = new ProjNet.CoordinateSystems.CoordinateSystemFactory();
            var epsg4326 = cf.CreateFromWkt("GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.01745329251994328,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]]");
            var epsg3785 = cf.CreateFromWkt("PROJCS[\"Popular Visualisation CRS / Mercator\", GEOGCS[\"Popular Visualisation CRS\", DATUM[\"Popular Visualisation Datum\", SPHEROID[\"Popular Visualisation Sphere\", 6378137, 0, AUTHORITY[\"EPSG\",\"7059\"]], TOWGS84[0, 0, 0, 0, 0, 0, 0], AUTHORITY[\"EPSG\",\"6055\"]],PRIMEM[\"Greenwich\", 0, AUTHORITY[\"EPSG\", \"8901\"]], UNIT[\"degree\", 0.0174532925199433, AUTHORITY[\"EPSG\", \"9102\"]], AXIS[\"E\", EAST], AXIS[\"N\", NORTH], AUTHORITY[\"EPSG\",\"4055\"]], PROJECTION[\"Mercator\"], PARAMETER[\"False_Easting\", 0], PARAMETER[\"False_Northing\", 0], PARAMETER[\"Central_Meridian\", 0], PARAMETER[\"Latitude_of_origin\", 0], UNIT[\"metre\", 1, AUTHORITY[\"EPSG\", \"9001\"]], AXIS[\"East\", EAST], AXIS[\"North\", NORTH], AUTHORITY[\"EPSG\",\"3785\"]]");
           var ct = ctf.CreateFromCoordinateSystems(epsg4326, epsg3785);
           foreach (System.Data.DataRow row in ds.Tables[0].Rows)
               if (row["X"] == DBNull.Value || row["Y"] == DBNull.Value) continue;
               double[] coords = new double[] { Convert.ToDouble(row["X"]), Convert.ToDouble(row["Y"]) };
               coords = ct.MathTransform.Transform(coords);
               row["X"] = coords[0];
               row["Y"] = coords[1];
          //Create Xlslabellayer and add it into map
           xlsLabelLayer = new SharpMap.Layers.LabelLayer("XLSLabel");

           xlsLabelLayer.DataSource = xlsProvider;

           xlsLabelLayer.LabelColumn = "Name";
           xlsLabelLayer.Style.ForeColor = Color.Black;
           xlsLabelLayer.Style.Font = new Font(FontFamily.GenericSerif, 12);
           xlsLabelLayer.Style.HorizontalAlignment = SharpMap.Styles.LabelStyle.HorizontalAlignmentEnum.Left;
           xlsLabelLayer.Style.VerticalAlignment = SharpMap.Styles.LabelStyle.VerticalAlignmentEnum.Bottom;
           xlsLabelLayer.Style.BackColor = new System.Drawing.SolidBrush(Color.FromArgb(128, 255, 0, 0)); 
           xlsLabelLayer.Style.CollisionBuffer = new System.Drawing.SizeF(2f, 2f);
           xlsLabelLayer.Style.CollisionDetection = true;
           xlsLabelLayer.LabelFilter = SharpMap.Rendering.LabelCollisionDetection.ThoroughCollisionDetection;
            //Add layer to map

            //Zoom to map extents

        public void RefreshMap()
            //--> use SharpMap to generate the map image            
            mapImage.Image = _globalMap.GetMap();

        private void btnZoomIn_Click(object sender, EventArgs e)
            //--> zoom in by changing the map width
            _globalMap.Zoom -= _globalMap.Zoom * ZOOM_FACTOR;            

        private void btnZoomOut_Click(object sender, EventArgs e)
            //--> zoom out by changing the map width
            _globalMap.Zoom += _globalMap.Zoom * ZOOM_FACTOR;         

        private void btnZoomFull_Click(object sender, EventArgs e)
            //--> zoom to the full extent of the map

        void mapImage_MapQueried(SharpMap.Data.FeatureDataTable data)
                if (data != null && data.Rows != null && data.Rows.Count > 0 && Convert.ToString(data.Rows[0].ItemArray[3]) != null)
                    PopupForm popup = new PopupForm(data.Rows[0].ItemArray[3].ToString(), data.Rows[0].ItemArray[3].ToString());

            //--> recenter map
                _globalMap.Center.X = zoomAroundPoint.X;
                _globalMap.Center.Y = zoomAroundPoint.Y;

      //->function to scroll the map and label
        private void trackBar_Scroll(object sender, EventArgs e)
            var mat = new System.Drawing.Drawing2D.Matrix();
            float angle = trackBar.Value;
            xlsLabelLayer.Style.Rotation =-angle;           
            _globalMap.Center.X = clickPoint.X;
            _globalMap.Center.Y = clickPoint.Y;
            _globalMap.MapTransform = mat;          

        //Refresh the map image if window is maximize nad minimize
        private void mapImage_SizeChanged(object sender, EventArgs e)

        private void checkBox_label_CheckedChanged(object sender, EventArgs e)
            //if labels have to be hide, remove the label layer from map
            if (checkBox_label.Checked)

            mapImage.Map = _globalMap;


        private void mapImage_MouseClick(object sender, MouseEventArgs e)
            //--> convert mouse click point from image coordiantes to world coordinates
            zoomAroundPoint = _globalMap.ImageToWorld(e.Location);


        private void Btn_Search_Click(object sender, EventArgs e)




Jul 13, 2011 at 12:00 PM

You cannot query TileLayer for location.

you need to use some geocoder like e.g.

Hth FObermaier

Jul 14, 2011 at 9:50 AM

Hi Hth FObermaier,

Do We have some example for tilelayer?

Please guide me how we can do the search for the tile layer.




Jul 14, 2011 at 4:07 PM

There is no sample code, because you can't query TileLayer for geolocation.