Tuesday, December 30, 2014

How to display the Rectangle In the Google Maps using Asp.net C#

Design Source file:-
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Rectangle.aspx.cs" Inherits="Maps.Rectangle" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false&key=Need to include API Key here"></script>
    <script type="text/javascript" src="../JavaScript/jquery.1.5.min.js"></script>
    <%--<script type="text/javascript" src="../JavaScript/jquery-1.8-ui.min.js"></script>
    <script type="text/javascript" src="../JavaScript/jquery.jqGrid.min.js"></script>
    <script type="text/javascript" src="../JavaScript/jquery.treeview.min.js"></script>
    <script type="text/javascript" src="../JavaScript/markerclusterer.js"></script>--%>
    <script type="text/javascript">
        var map;
        var MapCitys = [];

        function CreatingRectangle(GrpName, ArName) {
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "Rectangle.aspx/BindMapPoints",
                data: '{name: "' + GrpName + '",name1: "' + ArName + '" }',
                dataType: "json",
                success: function (data) {
                    var mapProp = {
                        center: new google.maps.LatLng(data.d[0].Latitude_SW, data.d[0].Longitude_SW),
                        zoom: 14,
                        mapTypeId: google.maps.MapTypeId.ROADMAP
                    };
                    map = new google.maps.Map(document.getElementById("idgoogleMap"), mapProp);

                    for (var i = 0; i < data.d.length; i++) {
                        //debugger;

                        var rectangle = new google.maps.Rectangle({
                            strokeColor: "##0000FF",
                            strokeOpacity: 0.8,
                            strokeWeight: 2,
                            fillColor: "#0000FF",
                            fillOpacity: 0.35,
                            map: map,
                            bounds: new google.maps.LatLngBounds(
                             new google.maps.LatLng(data.d[i].Latitude_SW, data.d[i].Longitude_SW),
                             new google.maps.LatLng(data.d[i].Latitude_NE, data.d[i].Longitude_NE))
                        });
                        MapCitys.push(rectangle);
                    }
                },
                error: function (result) {
                    alert("Error");
                }
            });
        };
        google.maps.event.addDomListener(window, 'load', CreatingRectangle('', ''));

    </script>
    <table width="100%">
        <tr>
            <td>
                <div id="idgoogleMap" style="width: 925px; height: 600px" />
            </td>
        </tr>
    </table>
</asp:Content>


CS file Code:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Services;

namespace Maps
{
    public partial class Rectangle : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {


        }

        [WebMethod]
        public static MAPS[] BindMapPoints(string name, string name1)
        {
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            List<MAPS> lstFencingCircle = new List<MAPS>();
            try
            {

                dt.Columns.Add("Latitude_SW");
                dt.Columns.Add("Longitude_SW");
                dt.Columns.Add("Latitude_NE");
                dt.Columns.Add("Longitude_NE");
                dt.Rows.Add("13.553345", "79.534389", "13.544625", "79.540870");

                foreach (DataRow dtrow in dt.Rows)
                {
                    MAPS objMAPS = new MAPS();
                    objMAPS.Latitude_SW = dtrow["Latitude_SW"].ToString();
                    objMAPS.Longitude_SW = dtrow["Longitude_SW"].ToString();
                    objMAPS.Latitude_NE = dtrow["Latitude_NE"].ToString();
                    objMAPS.Longitude_NE = dtrow["Longitude_NE"].ToString();
                    lstFencingCircle.Add(objMAPS);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }


            return lstFencingCircle.ToArray();
        }

        public class MAPS
        {
            public string Latitude_SW;
            public string Longitude_SW;
            public string Latitude_NE;
            public string Longitude_NE;
        }
    }

}




No comments:

Post a Comment