Add lat/lng point’s to a path and draw it onto your GoogleMap application from codebehind
This is one example how you can add a points collection to a polyline and draw the path onto GoogleMapControl project. The points are in string format in the SQL database so they are convertet to double datatype before added to the polyline path.
ASPX page
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <%@ Page Title="" Language="C#" MasterPageFile="Markers.master" AutoEventWireup="false" CodeBehind="CodeBehind.aspx.cs" Inherits="GoogleMaps.Samples.Markers.CodeBehind" %> <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server"> <h2> Markers Code Behind </h2> <p> GoogleMap control markers code behind sample. </p> <div class="map-wrap"> <map:GoogleMap ID="GoogleMap1" runat="server" Latitude="42.1229" Longitude="24.7879" Zoom="6" EnableScrollWheelZoom="true" CssClass="map" Width="100%" Height="600px" FullscreenControl="true"> </map:GoogleMap> </div> </asp:Content> |
Codebehind
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | 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 |