Polyline Code-Behind Sample

A sample about ASP.NET Google Map Control adding polyline from code-behind.

This is a demo how to add a polylines to Google Map Control in the code behind.

Thank you for using the Development Build of ASP.NET Google Map Control to build Google Maps faster.
Purchase the Commercial Build now to get access to all product updates and the ASP.NET Google Map Control expert support.

Source

Markup

<%@ Page Language="C#" MasterPageFile="~/samples/Polylines/Polylines.master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="GoogleMaps.Samples.samples.Polylines.CodeBehind.Default" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <div class="jumbotron">
        <h2>Polyline Code-Behind Sample</h2>
        <p>
            A sample about ASP.NET Google Map Control adding polyline from code-behind.
        </p>
    </div>
    <p>
        This is a demo how to add a polylines to Google Map Control in the code behind.
    </p>

    <div class='map-wrap'>
        <map:GoogleMap ID='GoogleMap1' runat='server' Width='100%' Height='600px' Latitude='42.1229'
            Longitude='24.7879' Zoom='7' CssClass='map' FullscreenControl="true">
        </map:GoogleMap>
    </div>

    <script type='text/javascript'>
        function handleClick(sender, e) {
            if (console) console.dir(e);
        }
    </script>
    
</asp:Content>

Code Behind

using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using GoogleMaps.Polylines;

namespace GoogleMaps.Samples.samples.Polylines.CodeBehind
{
    public partial class Default : System.Web.UI.Page
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            var line = new GooglePolyline
            {
                Clickable = true,
                Draggable = true,
                Editable = true,
                StrokeWeight = 10,
                Path = new List
                {
                    new LatLng(42.14304, 24.74967),
                    new LatLng(42.69649, 23.32601)
                }
            };

            // put some custom data
            line["Test"] = "Test me";

            // register click event handlers
            line.Click += HandleClick;

            // We do recommend adding GoogleMap component in the code on Page.Init
            GoogleMap1.Add(line);
        }

        protected void HandleClick(object sender, MouseEventArgs e)
        {
            var line = sender as GooglePolyline;
            // get and validate custom data
            var value = line["Test"] as string;
            Contract.Assert(value == "Test me");
        }
    }
}