Skip to content
Loading
 
0
  • Rajesh Gami
    -1
    You can follow below step to display Map
     
    Step 1 First You have already created Database, If not then create DB 
                Table Name = Locations
    1. Locations table fields  
    2.   
    3. Col_Name          Data Type  
    4.   
    5. Name                varchar(50)  
    6. Latitude             numeric(18,6)  
    7. Longitude          numeric(18,6)  
    8. Description         varchar(300)  
    Step 2 : In C# create aspx page
     
    Step 3: In aspx cs file write below code 
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5. using System.Web.UI;  
    6. using System.Web.UI.WebControls;  
    7. using System.Data;  
    8. using System.Configuration;  
    9. using System.Data.SqlClient;  
    10.   
    11. public partial class CS : System.Web.UI.Page  
    12. {  
    13.     protected void Page_Load(object sender, EventArgs e)  
    14.     {  
    15.         if (!this.IsPostBack)  
    16.         {  
    17.             DataTable dt = this.GetData("select * from Locations");  
    18.             rptMarkers.DataSource = dt;  
    19.             rptMarkers.DataBind();  
    20.         }  
    21.     }  
    22.   
    23.     private DataTable GetData(string query)  
    24.     {  
    25.         string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;  
    26.         SqlCommand cmd = new SqlCommand(query);  
    27.         using (SqlConnection con = new SqlConnection(conString))  
    28.         {  
    29.             using (SqlDataAdapter sda = new SqlDataAdapter())  
    30.             {  
    31.                 cmd.Connection = con;  
    32.   
    33.                 sda.SelectCommand = cmd;  
    34.                 using (DataTable dt = new DataTable())  
    35.                 {  
    36.                     sda.Fill(dt);  
    37.                     return dt;  
    38.                 }  
    39.             }  
    40.         }  
    41.     }  
    42. }  
    Step 4 : Now add below html and script in your aspx page design
    1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="CS.aspx.cs" Inherits="CS" %>    
    2.     
    3. "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">    
    4. "http://www.w3.org/1999/xhtml">    
    5. "server">    
    6.         
    7.     
    8.     
    9.     "form1" runat="server">    
    10.     "text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false">    
    11.     "text/javascript">    
    12.     var markers = [    
    13.     "rptMarkers" runat="server">    
    14.         
    15.              {    
    16.                 "title": '<%# Eval("Name") %>',    
    17.                 "lat": '<%# Eval("Latitude") %>',    
    18.                 "lng": '<%# Eval("Longitude") %>',    
    19.                 "description": '<%# Eval("Description") %>'    
    20.             }    
    21.         
    22.         
    23.         ,    
    24.         
    25.         
    26.     ];    
    27.         
    28.     "text/javascript">    
    29.     
    30.         window.onload = function () {    
    31.             var mapOptions = {    
    32.                 center: new google.maps.LatLng(markers[0].lat, markers[0].lng),    
    33.                 zoom: 8,    
    34.                 mapTypeId: google.maps.MapTypeId.ROADMAP    
    35.             };    
    36.             var infoWindow = new google.maps.InfoWindow();    
    37.             var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);    
    38.             for (i = 0; i < markers.length; i++) {    
    39.                 var data = markers[i]    
    40.                 var myLatlng = new google.maps.LatLng(data.lat, data.lng);    
    41.                 var marker = new google.maps.Marker({    
    42.                     position: myLatlng,    
    43.                     map: map,    
    44.                     title: data.title    
    45.                 });    
    46.                 (function (marker, data) {    
    47.                     google.maps.event.addListener(marker, "click", function (e) {    
    48.                         infoWindow.setContent(data.description);    
    49.                         infoWindow.open(map, marker);    
    50.                     });    
    51.                 })(marker, data);    
    52.             }    
    53.         }    
    54.         
    55.     "dvMap" style="width: 500px; height: 500px">    
    56.     
        
  •         
  •     
  •   
  • Check it and Run  
    -1