Drawing rectangles that scale to zoom level

Oct 15, 2010 at 3:57 PM
Edited Oct 15, 2010 at 4:00 PM

Hi. In the WinForm version of GMap, I draw a rectangle:

Public Overrides Sub OnRender(ByVal g As Graphics)
    Dim width As Integer = 100
    Dim height As Integer = 100
    Dim pen As New Pen(Brushes.Black)
   
    g.DrawRectangle(pen, LocalPosition.X, LocalPosition.Y, width, height)
End Sub

But when zooming in.out it remains the same size. How do I make it scale together with zoom level? (i.e. shrink as I zoom out and enlarge as I zoom in)

Thanks

Coordinator
Oct 15, 2010 at 4:09 PM

use geographic coordinates, check Demo.Clouds

Oct 18, 2010 at 8:43 AM
Edited Oct 18, 2010 at 8:44 AM

Hi Radioman. Thanks for the reply. I looked at Demo.Clouds but couldnt find anything helpful? I'm probably missing something. I did find this code though:

Public Overrides Sub OnRender(ByVal g As Graphics)
    Dim width As Integer = 100
    Dim height As Integer = 100
    Dim pen As New Pen(Brushes.Black)
   

   Dim scaledWidth As Single = Width / GMap.Projection.GetGroundResolution(GMap.Zoom, Position.Lat)
Dim scaledHeight As Single = Height / GMap.Projection.GetGroundResolution(GMap.Zoom, Position.Lat)

    g.DrawRectangle(pen, LocalPosition.X, LocalPosition.Y, scaledWidth, scaledHeight)
End Sub

 

Which looks like is getting me nearer to finding the solution. However, it's not working properly.

Although the rectangle scales according to the zoom level, it's not scaling correctly.

By looking at that code do you perhaps see where I might be going wrong?

Thanks for all your support Radioman. It is much appreciated.

Coordinator
Oct 18, 2010 at 9:55 AM

in clouds demo check the function void mapControl_OnMapZoomChanged(), thats ll you need to do

Oct 18, 2010 at 11:25 AM
Edited Oct 18, 2010 at 11:45 AM

Thanks Radioman - got it! I can now draw rectangles like this:

Public Overrides Sub OnRender(ByVal g As Graphics)
        Dim pntTopLeft As New Drawing.Point(GMap.FromLatLngToLocal(TopLeftPnt).X, GMap.FromLatLngToLocal(TopLeftPnt).Y)
        Dim pntBottomRight As New Drawing.Point(GMap.FromLatLngToLocal(BottomRightPnt).X, GMap.FromLatLngToLocal(BottomRightPnt).Y)
        Dim Width As Integer = pntBottomRight.X - pntTopLeft.X
        Dim Height As Integer = pntTopLeft.Y - pntBottomRight.Y

        g.FillRectangle(gFillBrush, LocalPosition.X, LocalPosition.Y, Width, Height)
End Sub

This works perfectly, except for one curious behaviour. After drawing the rectangle, if I pan the map to the point where the rectangle starts dissapearing
off the sides of the map, (after about 20% of the left or top edge of the rectangle has moved off the map), the entire rectangle dissapears!

Don't know if I'm expressing myself correctly? What I mean is if a start panning the map, the rectangle I drew obviously moves with the map - and that is fine.
But as soon as the rectangle moves past the edge of the GMap border, (well after about 20% of the rectangle has moved past the edge), the entire rectangle
suddenly dissapears!

This only happens when either the top or the left edges move past the map borders.

What I've noticed is that when the rectangle dissapears, it is because the OnRender event stops firing.
So in summary, the OnRender event stops triggering when the top-left corner of the rectangle is outside the map bounds. Any ideas?

Coordinator
Oct 18, 2010 at 11:55 AM

i see you didn't check clouds demo, anyway set for marker DisableRegionCheck = true;

Oct 18, 2010 at 6:42 PM

Thanks radioman, that did the trick. For the record, I did check out the clouds demo, but I guess I missed the DisableRegionCheck thing. Again thanks for your help.

Coordinator
Oct 18, 2010 at 7:32 PM

roger ;}

Nov 23, 2010 at 9:06 AM

Morning Radioman,

I can't seem to find the clouds demo.. Is it removed from the source code, or have I just gone blind? :-)

 

Coordinator
Nov 23, 2010 at 5:55 PM

open solution, expand the testing group, and thats it ;} it's there