Forum

Discussion Topics for Googlemaps.NET – A Great Map plugin for Windows ASP.NET applications

Polygon click, how to catch the click

Citat

I have attached the code below..

Currently the code and data produces 7 polygons, each polygon is given a unique id.

 

Its working really well as the click event fires for each polygon and returns the lat,lng within the polygon, however what I would like to do is also return the polygon ID as well as the lat,lng.

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

         }

 

        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()

                polyname = "polygon" & polynum

                drawpoly(Val(myreader("name")))

                polynum = polynum + 1

            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)

 

        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

            Dim polyname = New GooglePolygon()

            polyname.ID = nameref

            polyname.Clickable = True

            polyname.TargetControlID = "GoogleMap1"

            polyname.FillColor = Color.Transparent

            polyname.FillOpacity = 1.0F

            polyname.StrokeColor = Color.Blue

            polyname.StrokeWeight = 2

            polyname.Paths = landarea

            GoogleMap1.Overlays.Add(polyname)

            AddHandler polyname.Click, AddressOf map_click

 

 

        Catch ex As SqlException

        Finally

            con.Close()

        End Try

 

 

 

    End Sub

Sub map_click(ByVal sender As Object, ByVal e As MouseEventArgs)

 

        PrintEvent(polyname, e)

    End Sub

    Sub PrintEvent(name As String, e As MouseEventArgs)

        ListBox1.Items.Add(String.Format("{0} event was fired (lat: {1}, lng: {2}).", name, e.Position.Latitude, e.Position.Longitude))

    End Sub

 

 

Citat

Then is is just a matter to keep the polygon id in some of the existing properties as work around.
For example that could be ZIndex and after the sender is casted to GooglePolygon you can get the ID from polygon ZIndex.