polygon with stripes on RadMap

1 Answer 23 Views
Map
Ammar
Top achievements
Rank 1
Ammar asked on 10 Dec 2021, 03:50 PM

Hi there,

can I draw a polygon with stripes (lines) on RadMap?

I know how to make a plain (one color) polygon but it is possible to have stripes on the polygon?

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 13 Dec 2021, 09:28 AM

Hello, Ammar,

The following KB article demonstrates how to draw a polygon shape in RadMap:
https://docs.telerik.com/devtools/winforms/controls/map/how-to/adding-pins-and-drawing-regions 

If you want to customize the fill drawing, it is necessary to create a derivative of the MapPolygon class and override its Paint method. I have prepared a sample code snippet for your reference which result is illustrated in the below screenshot. Note that this is just a sample approach and it may not cover all possible cases. Feel free to modify and extend it in a way which suits your requirements best. 

        public RadForm1()
        {
            InitializeComponent();

            string cacheFolder = @"..\..\cache";
            OpenStreetMapProvider osmProvider = new OpenStreetMapProvider();
            MapTileDownloader tileDownloader = osmProvider.TileDownloader as MapTileDownloader;
            tileDownloader.WebHeaders.Add(System.Net.HttpRequestHeader.UserAgent, "your application name");
            LocalFileCacheProvider cache = new LocalFileCacheProvider(cacheFolder);
            osmProvider.CacheProvider = cache;

            osmProvider.InitializationComplete += delegate(object sender, EventArgs e)
            {
                this.radMap1.BringIntoView(new PointG(40d, -99d), 4);
            };
            this.radMap1.MapElement.Providers.Add(osmProvider);
            MapLayer pinsLayer = new MapLayer("Pins");
            this.radMap1.Layers.Add(pinsLayer);
            this.radMap1.InputBehavior = new CustomMapInputBehavior();
        }

        public class CustomMapInputBehavior : MapInputBehavior
        {
            public override void OnDoubleClick(EventArgs e)
            {
                MouseEventArgs args = e as MouseEventArgs;
                if (args.Button == MouseButtons.Left && Control.ModifierKeys == Keys.Control)
                {
                    PointL point = new PointL(args.X - this.MapElement.PanOffset.Width, args.Y - this.MapElement.PanOffset.Height);
                    PointG location = MapTileSystemHelper.PixelXYToLatLong(point.X, point.Y, this.MapElement.ZoomLevel);
                    while (location.Longitude > 180)
                    {
                        location.Longitude -= 360;
                    }
                    if (this.MapElement.Layers[0].Overlays.Count > 1 || (this.MapElement.Layers[0].Overlays.Count > 0 && this.MapElement.Layers[0].Overlays[0] is MapPolygon))
                    {
                        List<PointG> points = new List<PointG>();
                        foreach (MapVisualElement element in this.MapElement.Layers[0].Overlays)
                        {
                            MapPin pin = element as MapPin;
                            if (pin != null)
                            {
                                points.Add(element.Location);
                            }
                            CustomMapPolygon pol = element as CustomMapPolygon;
                            if (pol != null)
                            {
                                points.AddRange(pol.Points);
                            }
                        }
                        points.Add(location);
                        this.MapElement.Layers[0].Clear();
                        CustomMapPolygon polygon = new CustomMapPolygon(points);
                        polygon.BackColor = Color.FromArgb(125, Color.LightGreen);
                        this.MapElement.Layers[0].Add(polygon);
                    }
                    else
                    {
                        MapPin pin = new MapPin(location);
                        this.MapElement.Layers[0].Add(pin);
                    }
                }
                else
                {
                    base.OnDoubleClick(e);
                }
            }
        }

        public class CustomMapPolygon : MapPolygon
        {
            public CustomMapPolygon(IEnumerable<PointG> outerBoundary) : base(outerBoundary)
            {
            }

            public override void Paint(Telerik.WinControls.Paint.IGraphics graphics, IMapViewport viewport)
            {
                MapVisualElementInfo info = this.GetVisualElementInfo(viewport);
                GraphicsPath path = info.Path.Clone() as GraphicsPath;

                Matrix matrixOffset = new Matrix();
                matrixOffset.Translate(viewport.PanOffset.Width + info.Offset.X, viewport.PanOffset.Height + info.Offset.Y);
                path.Transform(matrixOffset);

                long mapSize = MapTileSystemHelper.MapSize(viewport.ZoomLevel);
                Matrix matrixWraparound = new Matrix();
                matrixWraparound.Translate(mapSize, 0);

                StringFormat format = new StringFormat();
                format.Alignment = StringAlignment.Center;
                format.LineAlignment = StringAlignment.Center;

                for (int i = 0; i < viewport.NumberOfWraparounds; i++)
                {
                    RectangleF bounds = path.GetBounds();
                    FillPrimitiveImpl fill = new FillPrimitiveImpl(this, null);
                    //fill.PaintFill(graphics, path, bounds);

                    System.Drawing.Graphics g = graphics.UnderlayGraphics as System.Drawing.Graphics;

                    //https://docs.microsoft.com/en-us/dotnet/api/system.drawing.drawing2d.hatchbrush?view=dotnet-plat-ext-6.0
                    HatchBrush hBrush = new HatchBrush(
                                            HatchStyle.ForwardDiagonal,
                                            Color.Red,
                                            Color.FromArgb(255, 128, 255, 255));
                    //https://docs.microsoft.com/en-us/dotnet/api/system.drawing.graphics.fillpolygon?view=dotnet-plat-ext-6.0
                    g.FillPolygon(hBrush, path.PathPoints);

                    BorderPrimitiveImpl border = new BorderPrimitiveImpl(this, null);
                    border.PaintBorder(graphics, null, path, bounds);

                    graphics.DrawString(this.DrawText, bounds, this.Font, this.ForeColor, format, System.Windows.Forms.Orientation.Horizontal, false);

                    path.Transform(matrixWraparound);
                }
            }
        }

I hope this information helps.

Regards,
Dess | Tech Support Engineer, Principal
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Ammar
Top achievements
Rank 1
commented on 07 Jan 2022, 12:05 PM

Thanks Dess. This work perfectly fine. 
Tags
Map
Asked by
Ammar
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or