Add Markers

Topics: SharpMap Project, SharpMap v0.9 / v1.x, WinForms Controls
Oct 7, 2010 at 9:42 AM

Hi Sharpteam,

Just want to know how I can put markers on map. I mean one Image based upon latitude and longitude point.

Please let me know any code sample.

Thanks for your time.

Vivek Kumar

Coordinator
Oct 7, 2010 at 11:03 AM
Edited Jun 22, 2011 at 1:10 PM

Hello Vevek Kumar,

you can add markers on an OSM background map.

As I said before, depending on where your point data comes from you need to choose the provider.

If it is Excel, you need to

  • use System.Data.OleDb.OleDbDataAdapter to fill a regular System.Data.DataSet,
  • use SharpMap.Data.Providers.DataTablePoint provider for use of the above data within SharpMap

Furthermore you will need to create a VectorLayer with the provider, adjust the style (Symbol is null per default) and,
since you stated that your data is latlon you will need to reproject!

Here is a sample code for all this:

 

public void CreateMapWithExcelDataSource()
{
    var map = new SharpMap.Map(new System.Drawing.Size(700, 700));
            
    //Get background
    var osmLayer = new SharpMap.Layers.TileLayer(new BruTile.Web.OsmTileSource(), "OSM");
    map.Layers.Add(osmLayer);

    //Get data from excel
    var xlsPath = string.Format(XlsConnectionString, System.IO.Directory.GetCurrentDirectory(), "Cities.xls");
    var ds = new System.Data.DataSet("XLS");
    using (var cn = new System.Data.OleDb.OleDbConnection(xlsPath))
    {
        cn.Open();
        using (var da = new System.Data.OleDb.OleDbDataAdapter(new System.Data.OleDb.OleDbCommand("SELECT * FROM [Cities$]", cn)))
            da.Fill(ds);
    }

    //Set up provider
    var xlsProvider = new SharpMap.Data.Providers.DataTablePoint(ds.Tables[0], "OID", "X", "Y");
    var xlsLayer = new SharpMap.Layers.VectorLayer("XLS", xlsProvider);
    xlsLayer.Style.Symbol = new System.Drawing.Bitmap("DefaultSymbol.png");
            
    //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 epsg3857 = 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\",\"3857\"]]");
    xlsLayer.CoordinateTransformation = ctf.CreateFromCoordinateSystems(epsg4326, epsg3857);

    //Add layer to map
    map.Layers.Add(xlsLayer);

    //Zoom to map
    map.ZoomToExtents();
    var img = map.GetMap();
    img.Save("osmexcel.bmp");

}

Oct 7, 2010 at 4:20 PM

Hi FObermaier,

Thanks for your reply, its works.

One more question Is we can we have map rotation function, to rotate the map with 0 degree, 45 degree, and 90 degree, based upon the image click for windows form.

Please provide me some code example.

Thanks for the kind help.

 

Vivek Kumar

 

Oct 8, 2010 at 9:55 AM

HI Hth FObermaier,

I am trying to get a maker label, that label will contain some information of the marker( Example:  Marker Image on map, and marker image will have a label called "XYZ")

Is we can acheive this?

later on I want to have marker click, and marker click will have some pop up with some information of marker.

If you can see this example link http://econym.org.uk/gmap/example_elabel.htm

Example link is having marker with some label , and onclick of marker it giving one popup with some another information.

I am looking for same thing what is there in above link.

Please provide some code example.

I really aperciate your help.

Thanks for you time and support.

Regards,

Vivek Kumar

 

 

Coordinator
Oct 8, 2010 at 3:12 PM
Edited Oct 8, 2010 at 3:19 PM

Hello Vivek,

If you want labeling, use LabelLayer with your datasource. If you do rotate the map, be sure to rotate labels the opposite angle for them to appear horizontal. Something like:

 

var xlsLabelLayer = new SharpMap.Layers.LabelLayer("XLSLabel");
xlsLabelLayer.DataSource = xlsProvider;
xlsLabelLayer.LabelColumn = "Name";
xlsLabelLayer.PriorityColumn = "Population";
xlsLabelLayer.Style.CollisionBuffer = new System.Drawing.SizeF(2f, 2f);
xlsLabelLayer.Style.CollisionDetection = true;
xlsLabelLayer.LabelFilter = SharpMap.Rendering.LabelCollisionDetection.ThoroughCollisionDetection;
xlsLabelLayer.RotationColumn = "Rotation";
xlsLabelLayer.CoordinateTransformation = xlsLayer.CoordinateTransformation;
map.Layers.Add(xlsLabelLayer);

If you want additional information for the marker your cursor is hovering over, you'd have to handle mousehover event and perform a query on your map to get all the information concerning your marker.

 

In general, if you need guidance on SharpMap usage, take the WinFormSamples project as reference.

Hth FObermaier

 

Oct 10, 2010 at 6:52 PM

Hi Hth FObermaier,

Labeling works for the map. you are just amazing.Thank you very much.

One more thing Hth FObermaier how I can handle click event for the marker. I want to show marker information on click of marker with one small popup.

I tried but not worked, I do't  want to include JavaScript.

Please let me know code example.

Thanks again for your help and support.

Regards,

Vivek Kumar.

 

Coordinator
Oct 11, 2010 at 8:03 AM

Hello Vivek,

do you use either MapBox or MapImage control from SharpMap.UI project.

If you do, all you need to do is set the active tool to query, set the index
of the layer to query and you'll get a FeatureDataTable of those features
that intersect a boundingbox created from the click on the map.

If you don't, take the source of those controls as a source of inspiration.

Hth FObermaier

 

P.S: Hth ... Hope that helps

Oct 11, 2010 at 8:20 AM

Hi  Hth FObermaier,

Thanks for the reply. I tried Setting active tool to query instead of pan. it does't worked.

Please let me know what else I have to do.

Please let me know some code example.

Thanks for the help.

Vivek

Coordinator
Oct 11, 2010 at 8:51 AM

Hello Vivek,

you need of course handle the MapQueried event and build a tooltip from returned data.

In the WinFormsDemo project that event is handled and the returned data is bound to a flexgrid control.

That should get you started.

Hth FObermaier

Oct 11, 2010 at 10:02 AM

Hi Hth FObermaier,

Its not geeting started I tried .

I handled mapquired event still some issue with popups.

Coluld you please look in it, and provide me some code example.

Thanks,

Vivek

Coordinator
Oct 11, 2010 at 11:49 AM

Hello Vivek,

I just added the sample from above to the WinFormSamples project, and it works ok for me (except that the coordinate transformation is not working as expected).
Please check:

- QueryLayerIndex must be set to 1 (0 is the image)

- Does it work if rotation is 0, e.g. MapTransform is Identity

Cheers FObermaier

 

Oct 12, 2010 at 8:30 AM

Hi FObermaier,

Thanks for your reply. but I am not able find the sample form above to the winformsamples.

I am searching in source code tab under trunk.

let me know where you have kept file.

Thanks,

Vivek Kumar.

Oct 12, 2010 at 10:18 AM
Edited Oct 12, 2010 at 12:40 PM

 

 

Hi FObermaier,

Thanks for your reply. but I am not able find the sample form above to the winformsamples.

I am searching in source code tab under trunk.

let me know where you have kept file.

Thanks,

Vivek Kumar

Oct 12, 2010 at 12:39 PM

Hi FObermaier,

Thanks for your reply. but I am not able find the sample form above to the winformsamples.

I am searching in source code tab under trunk.

let me know where you have kept file.

Thanks,

Vivek Kumar

 

Coordinator
Oct 13, 2010 at 11:45 AM

Vivek,

its in the TileLayerSample.cs file, the function is called InitializeMapOsmWithXls

you need to click repeatedly on the TileLayer radio button to get to it.

Hth FObermaier

Oct 13, 2010 at 1:10 PM

Hi Hth FObermaier,

 

I run the file, clicked twice got google lables, and set QueryLayerIndex  to 1 ,set query to active tool .

198 Pune 1775000 N 73.8522720336914 18.5357475280762
203 Mangalore 306078 N 75.1608810424805 12.9499998092651
202 Bangalore 2950000 N 77.5877304077148 12.9747505187988
100 Hyderabad 2750000 N 78.4850311279297 17.3945465087891
101 Madras 4475000 N 80.2478256225586 13.0615034103394

it saying this.

I need to have pop up on map click, with this all information not query index.

you can see http://econym.org.uk/gmap/example_elabel.htm here if you will click on any marker then it pping up some information

I am trying to do this.

is this is possible .

Please let me know some code example.

Thanks,

Vivek

 

Oct 13, 2010 at 4:12 PM

 

Hi Hth FObermaier,

I am still working on it. could you please also look in to it and check whthere this is possible or not.

Thanks,

Vivek