How to link drawings on on-line maps to the map
-
I'm using Visual C# 2010 Express. I've written code to display Provider maps on my form. I can draw lines on the maps, and capture the latitude / longitude from the map. My problem is that when I pan or zoom the map, my drawn line stays in one place; it doesn't move or resize with the map. I'm using a gMap control to load the provider maps as below:
private void Form1_Load(object sender, EventArgs e)
{
// Initialize map:
//gMapControl1.MapProvider = GMap.NET.MapProviders.BingMapProvider.Instance;
gMapControl1.MapProvider = GMap.NET.MapProviders.ArcGIS_Topo_US_2D_MapProvider.Instance;
GMap.NET.GMaps.Instance.Mode = GMap.NET.AccessMode.ServerOnly;
gMapControl1.Position = new GMap.NET.PointLatLng(39.401389, -077.986111); //mrb
gMapControl1.Zoom = 10;I've created an Overlay for my form, and I'm drawing on the overlay as below:
private void graphicalOverlay1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
Pen fl = new Pen(Color.Red, 3.0f);
e.Graphics.DrawLine(fl, x_start, y_start, xx, yy);}
Perhaps I shouldn't be drawing on the Overlay. But I don't know how to just draw on the providers image. Any direction to "lock" my drawn lines with the map would be appreciated. Thanks AW
-
I'm using Visual C# 2010 Express. I've written code to display Provider maps on my form. I can draw lines on the maps, and capture the latitude / longitude from the map. My problem is that when I pan or zoom the map, my drawn line stays in one place; it doesn't move or resize with the map. I'm using a gMap control to load the provider maps as below:
private void Form1_Load(object sender, EventArgs e)
{
// Initialize map:
//gMapControl1.MapProvider = GMap.NET.MapProviders.BingMapProvider.Instance;
gMapControl1.MapProvider = GMap.NET.MapProviders.ArcGIS_Topo_US_2D_MapProvider.Instance;
GMap.NET.GMaps.Instance.Mode = GMap.NET.AccessMode.ServerOnly;
gMapControl1.Position = new GMap.NET.PointLatLng(39.401389, -077.986111); //mrb
gMapControl1.Zoom = 10;I've created an Overlay for my form, and I'm drawing on the overlay as below:
private void graphicalOverlay1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
Pen fl = new Pen(Color.Red, 3.0f);
e.Graphics.DrawLine(fl, x_start, y_start, xx, yy);}
Perhaps I shouldn't be drawing on the Overlay. But I don't know how to just draw on the providers image. Any direction to "lock" my drawn lines with the map would be appreciated. Thanks AW
e.Graphics.DrawLine(fl, x_start, y_start, xx, yy);
uses pixel values, not geo coordinates. You have to get the latitude/longitude values for your lines, the transform them into pixels using the zoom and offset values of the underlying map. -
e.Graphics.DrawLine(fl, x_start, y_start, xx, yy);
uses pixel values, not geo coordinates. You have to get the latitude/longitude values for your lines, the transform them into pixels using the zoom and offset values of the underlying map.Bernhard; Thank you for reading my post question and replying. I'm getting the lat/long coordinates with this:
private void gMapControl1\_MouseClick(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { double lat = gMapControl1.FromLocalToLatLng(e.X, e.Y).Lat; double lng = gMapControl1.FromLocalToLatLng(e.X, e.Y).Lng; //------------------------------------------------
This returns decimal degrees which I then convert to dd-mm-ss.sss. I was using the following to paint my lines on the provider map, which does work to paint the lines;
private void gMapControl1\_Paint(object sender, PaintEventArgs e) { e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; Pen fl = new Pen(Color.Red, 3.0f); e.Graphics.DrawLine(fl, x\_start, y\_start, xx, yy);
But x_start, y_start, xx, yy are nothing more than screen coordinates; just like with the overlay code. And when I pan or zoom, the map moves but my drawn lines stay in the same place relative to the screen.
x\_start = e.X; y\_start = e.Y
Are you saying that I need to pass the geo coordinates to xx, yy to draw the lines on the providers map? I don't understand your statement; "then transform them into pixels using the zoom and offset values of the underlying map".
-
Bernhard; Thank you for reading my post question and replying. I'm getting the lat/long coordinates with this:
private void gMapControl1\_MouseClick(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { double lat = gMapControl1.FromLocalToLatLng(e.X, e.Y).Lat; double lng = gMapControl1.FromLocalToLatLng(e.X, e.Y).Lng; //------------------------------------------------
This returns decimal degrees which I then convert to dd-mm-ss.sss. I was using the following to paint my lines on the provider map, which does work to paint the lines;
private void gMapControl1\_Paint(object sender, PaintEventArgs e) { e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; Pen fl = new Pen(Color.Red, 3.0f); e.Graphics.DrawLine(fl, x\_start, y\_start, xx, yy);
But x_start, y_start, xx, yy are nothing more than screen coordinates; just like with the overlay code. And when I pan or zoom, the map moves but my drawn lines stay in the same place relative to the screen.
x\_start = e.X; y\_start = e.Y
Are you saying that I need to pass the geo coordinates to xx, yy to draw the lines on the providers map? I don't understand your statement; "then transform them into pixels using the zoom and offset values of the underlying map".
Oh, I think I understand what you meant! I need to do something like Double map_x equals gMapControl1.FromMapLatLngToLocal(PiontLatLng Point); Then when I pan or zoom, just redraw my lines. Can you provide the proper syntax for MapLatToLocal ?
-
Oh, I think I understand what you meant! I need to do something like Double map_x equals gMapControl1.FromMapLatLngToLocal(PiontLatLng Point); Then when I pan or zoom, just redraw my lines. Can you provide the proper syntax for MapLatToLocal ?
That's the way to go. But I do not know the functions provided by the control. Why don't you ask the author - there is a section below his article for taht purposes, isn't it?