Add dynamic map content from codebehind using C# or VB and a SQL database.

This simple example code show you the syntax how to read data from a SQL server table and add it to a Google Map Control webforms project and visualize your data. You can add any content to a map like labels, html-infowindows, mapmarkers, polylines, polygones, directions, regular infowindows that is created in codebehind.

EXAMPLE !!!

Imports System.Data.SqlClient
Imports System.Drawing
Imports GoogleMaps
Imports GoogleMaps.Overlays
Imports GoogleMaps.Drawing

Public Class land
Inherits System.Web.UI.Page
Dim s As String

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

‘ s = Request.QueryString(”comp”)
s = ”onuku”
Session(”field1”) = s

Dim sw As New LatLng
Dim ne As New LatLng

Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings(”daypilot”).ConnectionString)
Try
con.Open()
Dim sql As String = ”SELECT min(lat) as minlat, min(lng) as minlng, max(lat) as maxlat, max(lng) as maxlng FROM land where owner = ‘” & s & ”‘”
Dim cmd As New SqlCommand(sql, con)
Dim myreader As SqlDataReader = cmd.ExecuteReader()

While myreader.Read()
sw = New LatLng(Val(myreader(”minlat”)), Val(myreader(”minlng”)))
ne = New LatLng(Val(myreader(”maxlat”)), Val(myreader(”maxlng”)))
End While

Catch ex As SqlException
Finally
con.Close()
End Try

GoogleMap1.Bounds = New Bounds() With {
.NorthEast = ne,
.SouthWest = sw
}

End Sub
Private Sub land_LoadComplete(sender As Object, e As EventArgs) Handles Me.LoadComplete

Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings(”daypilot”).ConnectionString)
Try
con.Open()
Dim sql As String = ”SELECT distinct name FROM land WHERE owner = ‘” & s & ”‘ ORDER BY name”
Dim cmd As New SqlCommand(sql, con)
Dim myreader As SqlDataReader = cmd.ExecuteReader()
Dim landarea As New List(Of LatLng)()
While myreader.Read()
drawpoly(Val(myreader(”name”)))
End While
Catch ex As SqlException
Finally
con.Close()
End Try
End Sub

Sub drawpoly(ByRef nameref As Double)

Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings(”daypilot”).ConnectionString)
Dim polygon = New GooglePolygon()
Try
con.Open()
Dim sql As String = ”SELECT lat, lng , description FROM land where name = ‘” & nameref & ”‘ and owner = ‘” & s & ”‘ ORDER BY ord”
Dim cmd As New SqlCommand(sql, con)
Dim myreader As SqlDataReader = cmd.ExecuteReader()
Dim landarea As New List(Of LatLng)()
While myreader.Read()
landarea.Add(New LatLng(Val(myreader(”lat”)), Val(myreader(”lng”))))
End While

polygon.ID = nameref
polygon.Clickable = True
polygon.TargetControlID = ”GoogleMap1”
polygon.FillColor = Color.Transparent
polygon.FillOpacity = 1.0F
polygon.StrokeColor = Color.Blue
polygon.StrokeWeight = 2
polygon.Paths = landarea
GoogleMap1.Overlays.Add(polygon)

Catch ex As SqlException
Finally
con.Close()
End Try
AddHandler polygon.Click, AddressOf Polygon_Clk
End Sub

Sub Polygon_Clk(ByVal sender As Object, ByVal e As MouseEventArgs)
PrintEvent(”click”, e)
‘Label2.Text = String.Format(”Polygon clicked at {0}”, e.Position.Latitude)

End Sub

Sub PrintEvent(name As String, e As MouseEventArgs)
Label2.Text = (String.Format(”{0} event was fired (lat: {1}, lng: {2}).”, name, e.Position.Latitude, e.Position.Longitude))
End Sub