Polygons and Events

This is as a response to a discussion issue here

This is "quick and dirty" sample of how to use together polygons and events of GooleMap control.

The sample page handles client clicks on google map and after the second click it will start drawing polygons on the google map using the last two clicks' locations as oposite points of the polygon.
Here is the code:

Markup

<%@ Page Language="C#" MasterPageFile="GoogleMap.master" AutoEventWireup="false" CodeFile="PolygonsAndEvents.aspx.cs"
    Inherits="PolygonsAndEvents" Title="Polygons and Events Sample" %>

<asp:Content runat="server" ContentPlaceHolderID="phContent">

    <script type="text/javascript">
    
        function __setLocation(overlay, point) {
            var latField = document.getElementById('<%= _lat.ClientID %>');
            var lngField = document.getElementById('<%= _lng.ClientID %>');
            latField.value = point.lat();
            lngField.value = point.lng();
            <%= this.ClientScript.GetPostBackEventReference(_btnSubmit, "") %>;
        }
    </script>

    <h1>
        Polygons and Events Sample
    </h1>
    <hr style="visibility: hidden;" />
    <asp:ScriptManager ID="ScriptManager" runat="server" EnablePartialRendering="true" AsyncPostBackTimeout="900">
    </asp:ScriptManager>
    <asp:UpdatePanel runat="server" ID="upTest">
        <ContentTemplate>
            <artem:GoogleMap ID="GoogleMap1" runat="server" Width="800px" Height="600px" Latitude="42.1229" Longitude="24.7879"
                Zoom="4" OnClientClick="__setLocation">
            </artem:GoogleMap>
            <asp:HiddenField ID="_lat" runat="server" />
            <asp:HiddenField ID="_lng" runat="server" />
            <asp:LinkButton ID="_btnSubmit" runat="server" Text=""></asp:LinkButton>
        </ContentTemplate>
    </asp:UpdatePanel>
</asp:Content>

Code-behind

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Artem.Web.UI.Controls;
using System.Data.SqlClient;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Drawing;
using System.Globalization;

public partial class PolygonsAndEvents : System.Web.UI.Page {

    CultureInfo _culture = CultureInfo.GetCultureInfo("en-US");
    int _index;
    GooglePoint[] _points;


    public GooglePoint[] Points {
        get {
            if (_points == null)
                _points = new GooglePoint[2];
            return _points;
        }
    }

    protected override void OnInit(EventArgs args) {
        base.OnInit(args);
        //
        RegisterRequiresControlState(this);
    }

    protected override void OnLoad(EventArgs e) {
        base.OnLoad(e);
        //
        double lat = 0;
        double lng = 0;

        if (!string.IsNullOrEmpty(_lat.Value))
            lat = Convert.ToDouble(_lat.Value, _culture.NumberFormat);
        if (!string.IsNullOrEmpty(_lng.Value))
            lng = Convert.ToDouble(_lng.Value, _culture.NumberFormat);
        if (lat > 0 && lng > 0) {
            Points[_index] = new GooglePoint(lat, lng);
            _index = (_index == 0) ? 1 : 0;
        }
        //
        if (Points[0] != null && Points[1] != null) {
            GoogleMap1.Polygons.Clear();
            GooglePolygon polygon = new GooglePolygon();
            polygon.FillColor = Color.Red;
            polygon.FillOpacity = .8F;
            polygon.StrokeColor = Color.Blue;
            polygon.StrokeWeight = 1;
            polygon.Points.Add(Points[0]);
            polygon.Points.Add(new GooglePoint(Points[0].Latitude, Points[1].Longitude));
            polygon.Points.Add(Points[1]);
            polygon.Points.Add(new GooglePoint(Points[1].Latitude, Points[0].Longitude));
            polygon.Points.Add(Points[0]);
            GoogleMap1.Polygons.Add(polygon);
        }
    }

    protected override void LoadControlState(object savedState) {

        Triplet state = savedState as Triplet;
        if (state != null) {
            base.LoadControlState(state.First);
            _index = (int)state.Second;
            string points = state.Third as string;
            if (!string.IsNullOrEmpty(points)) {
                double lat;
                double lng;
                int index = 0;
                string[] ps = points.Split(';');
                foreach (string p in ps) {
                    lat = 0;
                    lng = 0;
                    string[] pair = p.Split(':');
                    if (!string.IsNullOrEmpty(pair[0]))
                        lat = Convert.ToDouble(pair[0], _culture.NumberFormat);
                    if (!string.IsNullOrEmpty(pair[1]))
                        lng = Convert.ToDouble(pair[1], _culture.NumberFormat);
                    if (lat > 0 && lng > 0) {
                        Points[index++] = new GooglePoint(lat, lng);
                    }
                }
            }
        }
        else
            base.LoadControlState(savedState);
    }

    protected override object SaveControlState() {

        StringBuilder buff = new StringBuilder();
        foreach (GooglePoint point in Points) {
            if (point != null)
                buff.AppendFormat("{0}:{1};",
                    Convert.ToString(point.Latitude, _culture.NumberFormat),
                    Convert.ToString(point.Longitude, _culture.NumberFormat));
        }
        if (buff.Length > 0) buff.Length--;
        return new Triplet(base.SaveControlState(), _index, buff.ToString());
    }
}


You can see the live demo of this code here

Hope this helps.
Regards
Velio

Last edited Feb 22, 2008 at 6:36 PM by velio, version 4

Comments

No comments yet.