Fit polygon on map

Topics: Help, Windows Forms
Nov 17, 2012 at 7:14 PM


I'm trying to find out hoy to calculate the zoom level to fit a polygon on map

To center it, I calculate the centroid of the polygon an then apply it to the GmapControl Position

But I don't know how to set the Zoom Ratio

Can I found that in some demo ?

Thank for your Help

Nov 17, 2012 at 8:48 PM


Nov 18, 2012 at 8:44 AM

Thank a lot RadioMan

I should have browse better the different Map Methods ...

It works fine But I still have a little problem : The first call to SetZoomToFitRect  after initialising the Map doesn't give the correct Zoom

I probably miss some event to watch but I dont know which ?

Eplanation : From a form (Frm1),  I instantiate the MapForm (Frm2) which contain the GmapControl on which a Polygon Overlay is Added.
After calling Show() of the MapForm from Frm1, I call a method to add a Polygon to the Polygon overlay and SetZoomToFitRect for that polygon

But this time (First call) the zoom cover an area 10 times greater to the expected one !
However, any subsequent call to the method to add and zoom polygon gives the good result !

What can be wrong for the first call ?

Thanks for your help




Nov 18, 2012 at 9:45 AM

add your markers/polygons in constructor, and call SetZoomToFitRect in MainForm_Load event

Nov 18, 2012 at 10:40 AM

Thank for your help RadioMan

I understand but  I  think it will be a bit more complicate to fix it  because my design is some different

1- In a main Application form it is possible to select some area (Polygon) and display it on a  Map, but the Map Form is not open before the first request
2- There are +/- 600 Polygon than can be selected but only a few need to be selected / displayed in one session
3- So at the first request, I Instantiate the MapForm then call a method to add the polygon (in a Dictionary) and zoom on it
4- For any subsequent request the form is already instanciated  so I just call the method to add a polygon and zoom on It
5- If the polygon is already in the Dictionary I just zoom on it

So the problem is probably because a the first call the Zoom Occurs before the Map is already Displayed and fixing that in such design can be a bit tricky or messy

Nov 18, 2012 at 10:49 AM

i'm welcome for solution, the thing is that map size is unknown before it's open

Nov 18, 2012 at 12:11 PM

Hello Radioman

I've found  a solution !

Basicaly I use :

1- A stack of RectLatLng needed for Zoom
2- A flag set when Map Form is Shown
3- A method Calling SetZoomToFitRect  with condition (IsShown Set and Stack not empty)

So I call this Method after each new Polygon and on after Shown Event

Here is some details

// Definition of the stack and flag

    Stack<RectLatLng> stkZoom = new Stack<RectLatLng>();
    public bool IsShown = false;

// Method to Zoom

    public void ZoomStack()
      if (!this.IsShown)
      RectLatLng R = stkZoom.Pop();

So when Adding a new Polygon I do :


And on the Swhown envent of the MapForm I do :

    private void FrmMapGoogle_Shown(object sender, EventArgs e)
      MapManager.IsShown = true;

 NB : I've created a derivated C_GMapPolyGon class including Bound property

It Would be nice to have a Shown event or flag on the GMap control

Nov 18, 2012 at 12:23 PM

yeah, that the way, but it doen't solve internal problem..

Nov 18, 2012 at 12:58 PM

Hello Radioman

What do you mean by  "internal problem"

Nov 18, 2012 at 5:14 PM

well i finally fixed it, basically it's the same technique, but inside control

meaning you can call SetZoomToFitRect before map is loaded