我正在开发一个包含 Google map 的 mvc 项目
我的系统允许用户向系统发送推文并将其保存在数据库中,然后在 map 上显示推文
我需要做的是“系统获取新数据时自动更新 map 上的标记”
有什么建议吗?非常感谢^^
最佳答案
您需要服务器上的脚本,给定时间戳将检查在该时间戳之后是否有新记录插入数据库。如果是,脚本应返回包含新信息的响应。
然后您应该使用 normal 向服务器端脚本发起 AJAX 请求。或 long polling , 带有最后一次更新的时间戳参数。
当您的 AJAX 请求从服务器接收到新信息时,您只需将新标记添加到 map 即可。然后使用更新的时间戳参数发起一个新的 AJAX 请求。
使用 jQuery 的伪示例:
var lastUpdate = ''; // You need to decide what the server should return
// when you load the map the first time.
function autoUpdate() {
$.ajax({
type: "GET",
url: "check_updates.aspx?last_update=" + lastUpdate,
dataType: 'json',
success: function(jsonData) {
// 1. Check if jsonData is empty. If empty skip to 4.
// Else we received some fresh data.
//
// 2. Update lastUpdate from the jsonData with the timestamp from
// the server. Don't use JavaScript to update the timestamp,
// because the time on the client and on the server will
// never be exactly in sync.
//
// 3. Add new markers on Google Map.
//
// 4. Relaunch the autoUpdate() function in 5 seconds.
setTimeout(autoUpdate, 5000);
}
});
}
关于javascript - 如何创建 GoogleMap 标记自动更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3284781/