JavaScript example which takes the longitude and latitude, statically hard-coded:
I have to retrieve the values form database and show in maps as a marker, where the number of markers are dynamic. currently the information of the marker is dependent on the hard-coded values of longitude and latitude but the end result should be the values under the column latitude and longitude, and also the title of the marker should be viewed as Title from database.
<head>
<title>Google Map Integration</title>
</head>
<body>
<div>
<!-- Google Map [Div]-->
<div id="googleMap" style="width:auto; height:800px;"></div>
<!-- END: Google Map [Div]-->
</div>
<!--Google Map-->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
//Load Google-Map on [Page-Load]
google.maps.event.addDomListener(window, 'load', resetMap());
function resetMap() {
//Google-Map DefaultPosition
var defaultPostion = new google.maps.LatLng(3, 101);
//Set Google-Map properties:
var mapProperties = {
center: defaultPostion,
zoom: 5,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
//Load Google-Map on "googleMap" [div]
var map = new google.maps.Map(document.getElementById("googleMap"), mapProperties);
var locations = [
['Tool Tip: Dubai', 25.10789521446838, 55.1819798029785, '<div><p>Dubai .</p></div>'],
['Tool Tip: Houston', 29.9928829, -95.4859515, '<div><p>Houston.</p></div>'],
['Tool Tip: London', 51.51121389999999, -0.11982439999997041, '<div><p>London.</p></div>'],
['Tool Tip: Riyadh', 24.7116667, 46.72416670000007, '<div><p>Riyadh.</p></div>'],
['Tool Tip: Kuala Lumpur', +Latitude , +Longitude, '<div><p>Kuala Lumpur.</p></div>']
];
for (i = 0; i < locations.length; i++) {
var currentToolTip = locations[i][0]
var currentLatitude = locations[i][1]
var currentLongitude = locations[i][2]
var currentContent = locations[i][3]
var currentPostion = new google.maps.LatLng(currentLatitude, currentLongitude);
//Add the [Marker] in Google-Map
var marker = new google.maps.Marker({
position: currentPostion,
map: map,
title: currentToolTip
});
//Initialize [Info Window] for each [Marker]
var infoWindow = new google.maps.InfoWindow();
//Add Listener for [Marker] "click"
google.maps.event.addListener(marker,'click', (function(marker,currentContent){
return function() {
infoWindow.setContent(currentContent);
infoWindow.open(map,marker);
};
})(marker,currentContent));
}
}
</script>
<!--END: Google Map-->
My Controller, C# Code, Connecting to database
private DataTable GetData()
{
string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT Title, Longitude, Latitude FROM Article"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return dt;
}
}
}
}
}
}