Draw a polygon

Topics: Windows Forms
Feb 15, 2010 at 7:40 PM
Edited Feb 15, 2010 at 7:44 PM

I use GMap.Net in my program, and I add this function, draw a polygon

http://6312797563042362127-a-1802744773732722657-s-sites.googlegroups.com/site/zeqkprojects/zeqktools-maps/area1.PNG

http://6312797563042362127-a-1802744773732722657-s-sites.googlegroups.com/site/zeqkprojects/zeqktools-maps/area2.PNG

has the same behavior that the drawing of polygons maps.google.com 

http://zeqktools.googlecode.com/files/ZeqkTools.Maps.rar

(sorry for my english)

Coordinator
Feb 16, 2010 at 1:16 PM

nice idea, but it eats cpu ;} On each frame you create new list, calculate local points and convert it to array. Recalculation is needed only if map position or zoom is changing.

public override void OnRender(Graphics g)
{
         _localPoints = new List<Point>();
         foreach(var item in GeoPoints)
         {
            Position = item;
            _localPoints.Add(LocalPosition);
         }

         g.DrawPolygon(Pen, _localPoints.ToArray());
}
Mar 12, 2010 at 7:58 AM

Thanks for the work and I am now using the code to implement my project.

But  there is one problem.

If I add a tooltip to the object GMapMarkerPolygon and to set TooltipMode = MarkerTooltipMode.OnMouseOver,

the tooltip never shows.

Is it possible to modify the code so as to fix this bug?

Thank you.

 

Coordinator
Mar 12, 2010 at 1:35 PM

set the marker size ;} and i hope you modify this example and test it via memory prifiler

Mar 14, 2010 at 5:05 PM

I found that to set the marker size is to actually to define a rectangle (width x height), is it right.  When the mouse cursor is within the rectange, the code will set the ismouseover variable to true.

However, this algorithm seems not suitable for polygon.  So how can I modify the code to check whether the mouse cursor is within the polygon?

 

Coordinator
Mar 14, 2010 at 8:09 PM

thats a good question, hm..

Mar 15, 2010 at 12:00 PM

Hello tcmLeung,

I also use (many) polygons in my application and mouseover is working perfect. I use WPF together with Visual Basic 2010.

If i can be of any help, please let me know...

Heru

 

 

 

Mar 15, 2010 at 3:32 PM

Hi Heru,

very glad to know you can trigger the mouseover event when mouse is over polygons.

Can you provide some code how to do so?

Actually I am using C# to implement but I can understand Visual Basic 2008 very well.

Thank you very much.

Terence

 

 

 

Mar 16, 2010 at 9:08 AM

Finally I found the solution. Here I want to share to all:

1.  In GMapMarker.cs, add the following virtual function:

public virtual bool LocalPointInPolygon(Point p)
      {
          return false;
      }

 

2.  In GMapControl.cs, in the function protected override void OnMouseMove(MouseEventArgs e)

change this line --  if (m.LocalArea.Contains(e.X, e.Y) to the following

if (m.LocalArea.Contains(e.X, e.Y) || m.LocalPointInPolygon(e.Location))

 

3.  In the custom marker file GMapMarkerPolygon.cs, add the following function:

public override bool LocalPointInPolygon(Point p)
      {
          if (_localPoints == null) return false;

          int i, j = _localPoints.Count - 1;
          bool oddNodes = false;

          double ret;

          for (i = 0; i < _localPoints.Count; i++)
          {
              if (_localPoints[i].Y < p.Y  && _localPoints[j].Y >= p.Y
                || _localPoints[j].Y < p.Y && _localPoints[i].Y >= p.Y)
              {
                  ret=_localPoints[i].X + (p.Y - _localPoints[i].Y) / (_localPoints[j].Y - _localPoints[i].Y) * (_localPoints[j].X - _localPoints[i].X);
                  if (ret < p.X)
                  {
                      oddNodes = !oddNodes;
                  }
              }
              j = i;
          }
          return oddNodes;
      }

Here I want to give thanks to Darel Rex Finley  that provides the algorithm of detecting point inside a polygon in the following website:

http://alienryderflex.com/polygon/

Coordinator
Mar 16, 2010 at 10:10 AM

great! i think we need 'official' polygon support ;}

Mar 17, 2010 at 1:46 PM

very nice!

Mar 29, 2010 at 8:26 AM

hello,

First of all, thanks for a great map engine!

 

i've try several way to drawing polygon...

alternatively you can customize DrawRoutes(Graphics g) method in GMapOverlay.cs,

just add g.FillPath(..) method before g.DrawPath

 

the code would be like this:

hello,
First of all, thanks for a great map engine!
i've try several way to drawing polygon...
alternatively you can customize DrawRoutes(Graphics g) method in GMapOverlay.cs,
just add g.FillPath(..) method before g.DrawPath 
the code would be like this:

 

public class GMapOverlay
{
......
      /// <summary>
      /// is the route drawn as polygon?
      /// </summary>
	public bool IsFillRouteAsPolygon = false;
	......


	protected virtual void DrawRoutes(Graphics g)
	{
	........
               if(rp.PointCount > 0)
               {
               	  if(IsFillRouteAsPolygon == true)
                  	g.FillPath(YourPolygonFillBrush,rp);
                  g.DrawPath(RoutePen, rp);
               }	
	}
}

& here's the example how to use:
	MyTestPolygon = new GMapOverlay(MainMap, "MyTestPolygon");
	MyTestPolygon.IsFillRouteAsPolygon = true;

these works fine for me in Windows Forms but tested yet for other platforms

& off course no cpu boost

 

 

CMIIW

 

 

Coordinator
Mar 29, 2010 at 4:36 PM

works perfect, thanks!

Apr 25, 2010 at 8:00 PM

Hi radioman, did you implement it on the code? I want to replace my "mappoint" control who let the user dray a filled polygon for making "zone" on the map?

 

Coordinator
Apr 25, 2010 at 8:03 PM

check the demo ;}

Apr 25, 2010 at 8:08 PM

hmm I just checked the Demo.WindowsPresentation and I didn't see any button for that, only in windows form?

Coordinator
Apr 25, 2010 at 8:18 PM

yes

Apr 25, 2010 at 8:19 PM

hmm, so it's not working for the wpf version? I was using the wpf version because I was thinking it's the best for the system usage. did I make a mistake?

Coordinator
Apr 25, 2010 at 8:21 PM

the polygon is just a figure generated like a route, just a question of time to implement

Apr 25, 2010 at 8:23 PM

oh, okay. I was wondering if the new version will support the "change" of the polygon in realtime? :) like what mappoint do, when we click on a segment it split in 2 and put a new "point" and change the size of the polygon

Coordinator
Apr 25, 2010 at 8:34 PM

maybe ;}

Apr 26, 2010 at 2:54 PM
Edited Apr 27, 2010 at 3:58 AM

I added draw polygon functionallity, like maps.google.com draw tool

screen

http://6312797563042362127-a-1802744773732722657-s-sites.googlegroups.com/site/zeqkprojects/zeqktools-maps/polygon.PNG

test it in http://zeqktools.googlecode.com/files/Release%201.10.04.26.rar

 

Apr 26, 2010 at 11:23 PM

oh really nice zeqk, radioman any chance to include it? it's exactly what I need :)

Coordinator
Apr 27, 2010 at 7:26 PM

its included already, but not in wpf version ;}

Apr 27, 2010 at 11:47 PM

we can do the same thing he did? the small box on the line for we can resize the polygon?

Apr 27, 2010 at 11:49 PM

zeqk any chance you can share your code? :)

Apr 28, 2010 at 12:28 AM
Edited Apr 28, 2010 at 12:29 AM
yes! no problem download from http://zeqktools.googlecode.com/files/zeqktools1.10.04.26%20_src.zip my project home page http://code.google.com/p/zeqktools/ and svn checkout http://zeqktools.googlecode.com/svn/trunk/ zeqktools-read-only
May 5, 2010 at 1:58 PM

the GMapPolygon will have tooltip??

Coordinator
May 5, 2010 at 2:09 PM

nop

May 5, 2010 at 7:15 PM
Edited May 5, 2010 at 7:17 PM

I use a tooltip in OnDoubleClick event

 

        private void MainMap_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            PointLatLng point = MainMap.FromLocalToLatLng(e.X, e.Y);
            foreach (GMapPolygon polygon in top.Polygons)
            {
                if (Functions.PointInPolygon(point, polygon.Points.ToArray()))
                {
                    ToolTip tip = new ToolTip();
                    tip.SetToolTip(MainMap, polygon.Tag.ToString());
                    tip.Show("", this, 1000);
                }
            }
        }

 

 

May 6, 2010 at 8:36 AM

Hmm, is there any way for changing the color inside the polygon?

Coordinator
May 6, 2010 at 9:10 AM

try set PolygonBackground in GMapOverlay

May 6, 2010 at 5:34 PM

Hi, thank mean that will change all the background of all the polygon in this overlay, no?

Coordinator
May 7, 2010 at 5:58 AM

yes

May 7, 2010 at 6:43 PM

Oh, that's bad, I'm in my Taxi Application, I need to load like 50 polygon for recreate "Zones", each of them have a color... so by changing the overlay color that will change all the polygon color, and I don't think to create 1 overlay for each polygon will be a great idea. So why not adding public Color FillColor; to the class GMapPolygon and them replace the g.FillPath(PolygonBackground, rp); by something like

                  if (r.FillColor)
                    g.FillPath(new SolidBrush(r.FillColor), rp);
                  else
                    g.FillPath(PolygonBackground, rp);

 

or at least g.FillPath(new SolidBrush(r.Color), rp);

 

what do you think?

 

Coordinator
May 7, 2010 at 10:52 PM
Edited May 7, 2010 at 11:44 PM

good idea ;} done!