基于时间的图层

尝试一下在线预览

此示例演示如何在指定的时间范围内显示来自 MapImageLayer 的流量信息。您可以通过更改时钟微件上的时间来查看不同时间的流量信息。

此应用程序显示动态交通地图服务,具有可视化流量速度和事件的功能。历史、实时和预测流量数据通过流量源每五分钟更新一次。您可以在此处了解有关数据的更多信息。

工作原理

流量服务需要令牌进行身份验证。此示例使用 apiKey 进行身份验证。您可以将其替换为您自己的 apiKey,也可以将其删除并在出现提示后登录。或者,您可以使用其他身份验证方法来访问流量服务。

       
1
2
3
4
5
6
7
const trafficLayer = new MapImageLayer({
  url: "https://traffic.arcgis.com/arcgis/rest/services/World/Traffic/MapServer",
  dpi: 48,
  imageFormat: "png32",
  refreshInterval: 3, // refresh the layer every 3 minutes
  useViewTime: false // layer sets its time extent and will ignore view's timeExtent.
});

初始化应用后,用户可以更改时钟微件上的时间,以显示该时间段的流量数据。时钟微件初始化如下图所示:

      
1
2
3
4
5
6
var clock = new Clock({
  el: "clock", // container div
  skin: "clock.svg",
  time: Date.now() // number of milliseconds elapsed since January 1, 1970, 00:00:00 UTC.
});
clock.on("time-change", setDate);

每当用户更改时钟微件上的时间时,都会触发 time-change 事件并更新流量图层的 timeExtent。如果在时钟上单击 live 按钮,则该图层将获取最新的流量数据。

              
1
2
3
4
5
6
7
8
9
10
11
12
13
14
function setDate(time) {
  // reset the clock widget show the latest traffic data if the user
  // clicks on `live` button on the clock
  if (clock.mode === "live") {
    trafficLayer.timeExtent = null;
  }
  else { // otherwise shows the traffic data where
    // the clock handles move to
    trafficLayer.timeExtent = {
      start: time,
      end: time
    };
  }
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.