【发布时间】:2014-01-23 23:52:06
【问题描述】:
我在 SQL 中有 Trucks 表。我的 Trucks 每 10 秒将 Lat 和 Lng 发送到 SQL,就像这张表一样
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 . . .
我通过使用 JSON.Parse 从 SQL 获取数据在谷歌地图上创建了标记。但我想刷新标记并将它们移动到地图上。(不像按 F5。有计时器或超时)。我不知道怎么做我在这些代码中使用了计时器或超时。
源码: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);
}
}
}
【问题讨论】:
-
谢谢,但我不清楚。
-
我正在使用 asp.net 和 sql 先生
标签: c# javascript google-maps google-maps-api-3