I have Trucks table in SQL.My Trucks send Lat and Lng to SQL every 10 seconds like this table
dbo.Trucks
ID readTime TruckID Lat Lng
1 2014-01-24 18:02:47.983 78 36,785 35,4672
2 2014-01-24 18:03:11.983 78 34,785 37,341
3 2014-01-24 18:03:45.541 78 31,785 34,242
.
.
780 2014-01-24 22:42:45.541 . . .
I created markers on google map by get data from SQL using JSON.Parse.But I want to refresh markers and move them on maps.(not like press F5.With a timer or time out).I dont know how can I use timer or timeout in these codes.
Source codes : http://www.aspdotnet-suresh.com/2013/05/aspnet-show-multiple-markers-on-google.html
<script type="text/javascript">
function initialize()
{
var markers = JSON.parse('<%=OnlineTrucks() %>');
var mapOptions =
{
center: new google.maps.LatLng(markers[0].lat, markers[0].lng),
zoom: 5,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var infoWindow = new google.maps.InfoWindow();
var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
image = '/Images/truck.png';
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,
icon: image,
});
(function(marker, data) {
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>
C#
public string OnlineTrucks() {
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection("Data
Source=localhost;Initial Catalog=MyDB;Integrated Security=true"))
{
using (SqlCommand cmd = new SqlCommand("select
title=TruckID,lat=Lat,lng=Lat from Trucks", 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);
}
}
}