Quantcast
Channel: GMap.NET - Great Maps for Windows Forms & Presentation
Viewing all articles
Browse latest Browse all 3384

New Post: Adding text to a marker

$
0
0
Here's a code to draw a circle marker and write a number inside it and you can change the circle shape to any other shape in the OnRender() method, hope it help you
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Diagnostics
Imports GMap.NET.WindowsForms
Imports GMap.NET


    Public Class GMapMarkerCircle
        Inherits GMapMarker
#Region "Properties"

        ''' <summary>
        ''' The MapControl for the circle to be drawn in
        ''' </summary>
        Public Property MapControl() As GMapControl

        ''' <summary>
        ''' The pen for the outer circle
        ''' </summary>
        Public Property OuterPen() As Pen

        ''' <summary>
        ''' The brush for the inner circle
        ''' </summary>
        Public Property InnerBrush() As Brush

        ''' <summary>
        ''' The brush for the Text
        ''' </summary>
        Public Property TextBrush() As Brush

        ''' <summary>
        ''' The font for the text
        ''' </summary>
        Public Property TextFont() As Font

        ''' <summary>
        ''' The text to display inside of the marker 
        ''' </summary>
        Public Property Text() As String

        Private radius As Integer = 20
        ''' <summary>
        ''' The size of the circle
        ''' </summary>
        Public Property CircleRadius() As Integer
            Get
                Return Me.radius
            End Get
            Set(ByVal value As Integer)
                radius = value
                Me.Size = New Size(radius, radius)
                Offset = New Point(-Size.Width \ 2, -Size.Height \ 2)
            End Set
        End Property


#End Region

        ''' <summary>
        ''' Constructor
        ''' </summary>
        ''' <param name="outer">The pen for the outer ring</param>
        ''' <param name="inner">The brush for the inner circle.</param>
        ''' <param name="circleRadius">The radius in pixel of the whole circle</param>
        ''' <param name="text">The text in the marker.</param>
        Public Sub New(ByVal p As PointLatLng, ByVal outer As Pen, ByVal inner As Brush, ByVal circleRadius As Integer, ByVal text As String, ByVal mapCtrl As GMapControl)
            MyBase.New(p)
            Me.MapControl = mapCtrl
            Me.OuterPen = outer
            Me.InnerBrush = inner
            Me.CircleRadius = circleRadius
            Me.Text = text
            Me.TextFont = New Font("Tahoma", CInt(circleRadius \ 2), FontStyle.Regular)
            Me.TextBrush = Brushes.Black
            Me.Offset = New System.Drawing.Point(-Size.Width \ 2, -Size.Height \ 2)
        End Sub

        ''' <summary>
        ''' Constructor
        ''' </summary>
        ''' <param name="p">The LatLongPoint of the marker.</param>
        ''' <param name="outer">The pen for the outer ring</param>
        ''' <param name="inner">The brush for the inner circle.</param>
        ''' <param name="circleRadius">The radius in pixel of the whole circle</param>
        ''' <param name="textBrush">The brush for the text.</param>
        Public Sub New(ByVal p As PointLatLng, ByVal outer As Pen, ByVal inner As Brush, ByVal circleRadius As Integer, ByVal text As String, ByVal textBrush As Brush, ByVal mapCtrl As GMapControl)
            Me.New(p, outer, inner, circleRadius, text, mapCtrl)
            Me.TextBrush = textBrush
        End Sub

        ''' <summary>
        ''' Constructor
        ''' </summary>
        ''' <param name="p">The LatLongPoint of the marker.</param>
        ''' <param name="outer">The pen for the outer ring</param>
        ''' <param name="inner">The brush for the inner circle.</param>
        ''' <param name="circleRadius">The radius in pixel of the whole circle</param>
        ''' <param name="textBrush">The brush for the text.</param>
        Public Sub New(ByVal p As PointLatLng, ByVal outer As Pen, ByVal inner As Brush, ByVal circleRadius As Integer, ByVal text As String, ByVal textBrush As Brush, ByVal textFont As Font, ByVal mapCtrl As GMapControl)
            Me.New(p, outer, inner, circleRadius, text, textBrush, mapCtrl)
            Me.TextFont = textFont
        End Sub

        ''' <summary>
        ''' Render a circle
        ''' </summary>
        ''' <param name="g"></param>
        Public Overrides Sub OnRender(ByVal g As Graphics)
            g.DrawEllipse(OuterPen, New Rectangle(LocalPosition.X, LocalPosition.Y, radius, radius))
            g.FillEllipse(InnerBrush, New Rectangle(LocalPosition.X, LocalPosition.Y, radius, radius))
            

            ' Draw Text
            If Not String.IsNullOrEmpty(Me.Text) Then
                Dim sizeOfString As SizeF = g.MeasureString(Me.Text, Me.TextFont)
                Dim x As Integer = (LocalPosition.X + radius \ 2) - CInt(sizeOfString.Width \ 2)
                Dim y As Integer = (LocalPosition.Y + radius \ 2) - CInt(sizeOfString.Height \ 2)
                g.DrawString(Me.Text, Me.TextFont, Me.TextBrush, x, y)
            End If
        End Sub


        Private Function CalculateNewDiameterForZoomLevel(ByVal point As PointLatLng, ByVal Radius As Double) As Integer
            Dim groundResolution As Double = MapControl.MapProvider.Projection.GetGroundResolution(Convert.ToInt32(MapControl.Zoom), point.Lat)
            Dim rad As Integer = CInt(Fix(Radius * 2 / groundResolution))
            Return rad
        End Function

        Private Function TransformAndRotate(heading As Double, points() As PointF) As PointF()

            Dim cosRot As Double = Math.Cos((heading + 90) * Math.PI / 180)
            Dim sinRot As Double = Math.Sin((heading + 90) * Math.PI / 180)

            For i = 0 To points.Length - 1
                Dim x As Single = points(i).X
                Dim y As Single = points(i).Y
                points(i).X = CSng((LocalPosition.X) + (x * cosRot - y * sinRot)) 'simplified rotation and transformation matrix
                points(i).Y = CSng((LocalPosition.Y) + (x * sinRot + y * cosRot))
            Next

            Return points
        End Function

    End Class

Viewing all articles
Browse latest Browse all 3384


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>