Imports C1.Silverlight.Maps

Partial Public Class Grid
    Inherits UserControl
    Private vl As C1VectorLayer

    Public Sub New()
        InitializeComponent()

        AddHandler Me.Loaded, AddressOf Grid_Loaded
        AddHandler Me.Unloaded, AddressOf Grid_Unloaded
    End Sub

    Private Sub Grid_Unloaded(sender As Object, e As RoutedEventArgs)
        ' Release memory
        Me.maps.Layers.Clear()
    End Sub

    Private Sub Grid_Loaded(sender As Object, e As RoutedEventArgs)
        Dim fc As Color = Colors.LightGray
        Dim bk As Color = Color.FromArgb(&HFF, &H44, &H44, &H44)
        Dim bb As Color = Colors.Black
        maps.Foreground = New SolidColorBrush(fc)
        maps.Background = New SolidColorBrush(bk)
        maps.BorderBrush = New SolidColorBrush(bb)

        vl = New C1VectorLayer()

        Dim stroke As New SolidColorBrush(Colors.LightGray)

        For lon As Integer = -180 To 180 Step 30
            Dim dc As New DoubleCollection()
            dc.Add(1)
            dc.Add(2)

            Dim pl As New C1VectorPolyline() With {
                .Stroke = stroke,
                .StrokeDashArray = dc
            }
            Dim pc As New PointCollection()
            pc.Add(New Point(lon, -85))
            pc.Add(New Point(lon, +85))
            pl.Points = pc
            vl.Children.Insert(0, pl)

            Dim lbl As String = Math.Abs(lon).ToString() & "°"
            If lon > 0 Then
                lbl += "E"
            ElseIf lon < 0 Then
                lbl += "W"
            End If

            Dim pm As New C1VectorPlacemark() With {
                .GeoPoint = New Point(lon, 0),
                .Label = lbl,
                .LabelPosition = LabelPosition.Top
            }
            vl.Children.Add(pm)
        Next

        For lat As Integer = -80 To 80 Step 20
            Dim dc As New DoubleCollection()
            dc.Add(1)
            dc.Add(2)

            Dim pl As New C1VectorPolyline() With {
                .Stroke = stroke,
                .StrokeDashArray = dc
            }
            Dim pc As New PointCollection()
            pc.Add(New Point(-180, lat))
            pc.Add(New Point(180, lat))
            pl.Points = pc
            vl.Children.Insert(0, pl)

            Dim lbl As String = Math.Abs(lat).ToString() & "°"
            If lat > 0 Then
                lbl += "N"
            ElseIf lat < 0 Then
                lbl += "S"
            End If

            Dim pm As New C1VectorPlacemark() With {
                .GeoPoint = New Point(0, lat),
                .Label = lbl,
                .LabelPosition = LabelPosition.Right
            }
            vl.Children.Add(pm)
        Next

        maps.Layers.Add(vl)
    End Sub
End Class
