I want to add a text box with color to RadMap

1 Answer 74 Views
Map
Ammar
Top achievements
Rank 1
Iron
Ammar asked on 08 Sep 2022, 11:03 AM

Can I add a textbox on the map filled in a color of my choice just like in the attached photo ?

 

 

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 08 Sep 2022, 11:34 AM

Hi, Ammar,

The following KB article is quite useful about adding a pin with a text:

https://docs.telerik.com/devtools/winforms/knowledge-base/map-pin-with-text 

It is possible to extend the custom MapPin's rendering and just before calling the DrawString method, you can call FillRectangle passing the desired fill color and using the same rectangle for drawing the text.

            public override void Paint(IGraphics graphics, IMapViewport viewport)
            {
                object state = graphics.SaveState();
                graphics.ChangeSmoothingMode(SmoothingMode.AntiAlias);
                MapVisualElementInfo info = this.GetVisualElementInfo(viewport);
                GraphicsPath path = info.Path.Clone() as GraphicsPath;
                GraphicsPath dotPath = new GraphicsPath();
                long mapSize = MapTileSystemHelper.MapSize(viewport.ZoomLevel);
                Matrix matrixOffset = new Matrix();
                matrixOffset.Translate(viewport.PanOffset.Width + info.Offset.X, viewport.PanOffset.Height + info.Offset.Y);
                path.Transform(matrixOffset);
                Matrix matrixWraparound = new Matrix();
                matrixWraparound.Translate(mapSize, 0);

                for (int i = 0; i <= viewport.NumberOfWraparounds - 1; i++)
                {
                    RectangleF bounds = path.GetBounds();
                    float diameter = bounds.Width / 3 ;
                    dotPath.AddEllipse(bounds.X + diameter, bounds.Y + diameter, diameter, diameter);
                    graphics.FillPath(this.BorderColor, dotPath);
                    FillPrimitiveImpl fill = new FillPrimitiveImpl(this, null);
                    fill.PaintFill(graphics, path, bounds);
                    BorderPrimitiveImpl border = new BorderPrimitiveImpl(this, null);
                    border.PaintBorder(graphics, null/* TODO Change to default(_) if this is not a reference type */, path, bounds);

                    StringFormat stringFormat = new StringFormat();
                    stringFormat.Alignment = TelerikAlignHelper.TranslateAlignment(ContentAlignment.MiddleLeft);
                    stringFormat.LineAlignment = TelerikAlignHelper.TranslateLineAlignment(ContentAlignment.MiddleLeft);
                    SizeF size = RadGdiGraphics.MeasurementGraphics.MeasureString(this.Text, this.Font);
                    graphics.FillRectangle(new Rectangle((int)bounds.X, (int)bounds.Y, (int)size.Width + 10,
                        (int)size.Height), Color.Red);
                    graphics.DrawString(this.Text, new Rectangle((int)bounds.X, (int)bounds.Y, (int)size.Width + 10,
                        (int)size.Height), this.Font, this.ForeColor, stringFormat, Orientation.Horizontal, false);

                    path.Transform(matrixWraparound);
                }

                graphics.RestoreState(state);
            }

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.

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Principal
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
Map
Asked by
Ammar
Top achievements
Rank 1
Iron
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or