Rotated ellipse marker in gmap.net

50 Views Asked by At

I'm trying to implement a custom gmap.net marker, containing rotated ellipse. The marker receives short and long axes as well as an azimuth. The problem is that ellipses are displayed at wrong locations. When the map is moving, the overlay of ellipses stays on its place. I guess that the reason it happens is using of TranslateTransform and RotateTransform functions. When I'm commenting them out - all the markers are displayed at correct locations and behave correctly.

The following is an example of a code I'm using at OnRender override:

public override void OnRender(Graphics g) 

{ 

    int centerX = this.LocalPosition.X - ShortAxis / 2;
    int centerY = this.LocalPosition.Y - LongAxis) / 2;
    g.TranslateTransform(this.LocalPosition.X, his.LocalPosition.Y);
    g.DrawEllipse(Pen, centerX, centerY. ShortAxis, LongAxis);
    g.RotateTransform((float)(this.Azimuth % 360);
    g.ResetTransform();   

} 

I'm expecting ellipses will be drawn at correct locations and the overlay will behave like other map overlays.

1

There are 1 best solutions below

0
Roman Flickstein On

After researching this topic, I came up with the following code that does its job:

public override void OnRender(Graphics g)
{
    double scale = Overlay.Control.MapProvider.Projection.GetGroundResolution((int)Overlay.Control.Zoom, Position.Lat);
    int longAxisPixels = (int)(this.LongAxis / scale);
    int shortAxisPixels = (int)(this.ShortAxis / scale);
    float h2 = longAxisPixels / 2;
    float w2 = shortAxisPixels / 2;

    var gstate = g.Save();
    g.TranslateTransform(LocalPosition.X, LocalPosition.Y);
    g.RotateTransform(this.Azimuth);
    g.TranslateTransform(-LocalPosition.X, -LocalPosition.Y);
    g.DrawEllipse(new Pen(Color.Blue), new Rectangle((int)(LocalPosition.X - w2), (int)(LocalPosition.Y - h2), shortAxisPixels, longAxisPixels));
    g.ResetTransform();
    g.Restore(gstate);
}