javascript - 如何创建 GoogleMap 标记自动更新?

标签 javascript asp.net-mvc google-maps

我正在开发一个包含 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/

相关文章:

javascript - LocalStoragephonegap相机图像

c# - 在 C# ASP.NET MVC 中创建月份下拉列表

ios - 面向 'GMSServicesException' 的 Google map 已经在 Appdelegate 中提供了有效 key

javascript - :focus simultaneously with input of button and search

javascript - 使用 Jquery 删除整个模态框

c# - 如何在 ASP.NET WebAPI 项目中使用 MVC 路由

c# - 将 ID 号添加到 CSS 标签

reactjs - google.maps.places.PlacesService(map) 总是返回 null

javascript - 将 SqlDataReader 传递和解析到 javascript - 好的做法吗?

javascript - 输入上的 keyup 事件在页面加载时触发