Sunday, 23 June 2013

Show Multiple Markers on Google Map from Database using ASP.Net with JavaScript

In this article, I will explain how to show  or add multiple markers to Google maps from database in asp.net website using JavaScript or JavaScript Show multiple markers in Google maps using database in asp.net.

First you need create a new table in your database like as shown below:
Column Name
Data Type
Allow Nulls
PlaceID
int(set isidentity property=true)
No
PlaceName
varchar(50)
Yes
PlaceLatitude
numeric(12,6)
Yes
PlaceLongitude
numeric(12,6)
Yes
PlaceDescription
Varchar(250)
Yes

If table creation completed then enter some sample data such as follows..
PlaceID
PlaceName
PlaceLatitude
PlaceLongitude
PlaceDescription
1
Kolkata
22.5697
88.3697
This is Kolkata
2
Hyderabad
17.2667
78.5302
This is Hyderabad
3
Bangalore
12.8974
77.5195
This is Bangalore
4
Visakhapatnam
17.5183
83.3203
This is Visakhapatnam
5
Chennai
12.4974
83.3203
This is Chennai

Now write the following code in your aspx to show multiple markers in Google map from database like as shown below:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Show/Add multiple markers to Google Maps from database in asp.net website</title>
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0; padding: 0 }
#map_canvas { height: 100% }
</style>
<script type="text/javascript" src = "https://maps.googleapis.com/maps/api/js?key=AIzaSyC6v5-2uaq_wusHDktM9ILcqIrlPtnZgEk&sensor=false">
</script>
<script type="text/javascript">
function initialize() {
var markers = JSON.parse('<%=ConvertDataTabletoString() %>');
var mapOptions = {
center: new google.maps.LatLng(markers[0].lat, markers[0].lng),
zoom: 5,
mapTypeId: google.maps.MapTypeId.ROADMAP
//  marker:true
};
var infoWindow = new google.maps.InfoWindow();
var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
for (i = 0; i < markers.length; i++) {
var data = markers[i]
var myLatlng = new google.maps.LatLng(data.lat, data.lng);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: data.title
});
(function(marker, data) {

// Attaching a click event to the current marker
google.maps.event.addListener(marker, "click", function(e) {
infoWindow.setContent(data.description);
infoWindow.open(map, marker);
});
})(marker, data);
}
}
</script>
</head>
<body onload="initialize()">
<form id="form1" runat="server">
<div id="map_canvas" style="width: 500px; height: 400px"></div>
</form>
</body>
</html>


Now add following namespaces in code behind

C# Code

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
After that write the following code in code behind


// This method is used to convert datatable to json string
public string ConvertDataTabletoString()
{
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=positionmaster;Integrated Security=true"))
{
using (SqlCommand cmd = new SqlCommand("select title=PlaceName,lat=PlaceLatitude,lng=PlaceLongitude,PlaceDescription from LocationDetails", con))
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
Dictionary<string, object> row;
foreach (DataRow dr in dt.Rows)
{
row = new Dictionary<string, object>();
foreach (DataColumn col in dt.Columns)
{
row.Add(col.ColumnName, dr[col]);
}
rows.Add(row);
}
return serializer.Serialize(rows);
}
}
}

VB.NET Code

Imports System.Collections.Generic
Imports System.Data
Imports System.Data.SqlClient
Partial Class Default3
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
End Sub
' This method is used to convert datatable to json string
Public Function ConvertDataTabletoString() As String
Dim dt As New DataTable()
Using con As New SqlConnection("Data Source=localhost;Initial Catalog=positionmaster;Integrated Security=true")
Using cmd As New SqlCommand("select title=PlaceName,lat=PlaceLatitude,lng=PlaceLongitude,PlaceDescription from LocationDetails", con)
con.Open()
Dim da As New SqlDataAdapter(cmd)
da.Fill(dt)
Dim serializer As New System.Web.Script.Serialization.JavaScriptSerializer()
Dim rows As New List(Of Dictionary(Of String, Object))()
Dim row As Dictionary(Of String, Object)
For Each dr As DataRow In dt.Rows
row = New Dictionary(Of String, Object)()
For Each col As DataColumn In dt.Columns
row.Add(col.ColumnName, dr(col))
Next
rows.Add(row)
Next
Return serializer.Serialize(rows)
End Using
End Using
End Function
End Class


1 comments:

what is "JSON" in this code? thanks!

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More