如何创建Google地图标记自动更新?

3

我正在开发一个包含Google地图的MVC项目。

我的系统允许用户向系统发送推文并将其保存在数据库中,然后在地图上显示推文。

现在我需要做的是,“当系统获取新数据时自动更新地图上的标记”。

有什么建议吗?非常感谢^ ^

1个回答

4
您需要在服务器上编写一个脚本,给定一个时间戳,将检查是否在该时间戳之后插入了新记录到数据库中。如果是,则应该返回带有新信息的响应的脚本。
然后,您应该使用normallong polling之一,使用最后更新的时间戳参数向服务器端脚本发起AJAX请求。
当您的AJAX请求从服务器接收到新信息时,您只需将新标记添加到地图中。然后使用更新的时间戳参数启动新的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);
    }
  });
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接