Custom Marker Size & Offset Calculation within the OnRender Method

Topics: Windows Forms
Jul 12, 2012 at 8:37 PM
Edited Jul 12, 2012 at 8:38 PM

I created a custom marker type so that I could draw a circle around a given PointLatLng with a given radius in kilometers.

Because I am specifying the circle diameter in kilometers rather than pixels, I have to calculate the pixel distance on the fly when we render the marker based on the current zoom level and projection.

So I naturally put the Size and Offset calculations in the OnRender overriden method.  But it appears this can't be done because the marker is not shown with the proper offset until I move the map (rather then simply changing zoom level).

Anyone have any thoughts on how to better do this?  All I want to do is have a cirlce on the map with a set radius in kilometers that stays there as I pan and zoom.

Here is my complete custom marker class:

using System;
using System.Drawing;
using GMap.NET.WindowsForms;
using FAA.Spectrum.DirectionFinder.Controls;

namespace FAA.Spectrum.DirectionFinder.Markers
{
    public class GMapCircleMarker : GMapMarker
    {
        public Pen OuterPen { get; set; }
        public double RadiusInKM { get;set; }
        internal Map HostMap { get; set; }
        
        public GMapCircleMarker(GMap.NET.PointLatLng p)
            : base(p)
        {
            DisableRegionCheck = true;
            IsHitTestVisible = false;

            OuterPen = new Pen(Color.DarkRed, 2F);
            RadiusInKM = 0;
            HostMap = null;
        }
        
        public override void OnRender(Graphics g)
        {
            if (RadiusInKM > 0 & HostMap != null)
            {
                double groundResolution = HostMap.MapProvider.Projection.GetGroundResolution(Convert.ToInt32(HostMap.Zoom), Position.Lat);
                int diameter = (int)(RadiusInKM * 2 * 1000 / groundResolution);

                Size = new System.Drawing.Size(diameter, diameter);
                Offset = new System.Drawing.Point(-Size.Width / 2, -Size.Height / 2);

                g.DrawEllipse(OuterPen, new Rectangle(LocalPosition.X, LocalPosition.Y, diameter, diameter));
            }
        }
    }
}
Coordinator
Jul 16, 2012 at 3:35 PM

try map.Invalidate();

Nov 30, 2012 at 1:23 PM
Edited Nov 30, 2012 at 1:24 PM

Mike,

I think you can find your sollution in this topic: http://greatmaps.codeplex.com/discussions/402114