当缩放发生变化时,如何通过 OpenLayers 中止未完成的地图图块请求?

发布于 2024-12-21 18:52:49 字数 287 浏览 1 评论 0原文

我有一个带有 TMS 图层的 OpenLayers 地图。对于每次缩放更改,我的地图都会发出大约 56 个地图图块请求(取决于地图/屏幕尺寸)。如果用户快速放大或缩小,所有这些地图图块请求都会按顺序排队,我的理解是来自 OpenLayers 的地图图块请求不是异步调用的。因此,如果用户快速放大或缩小,最终会出现许多不再需要的地图图块请求,并且需要的地图图块将不会加载,直到发出所有先前的请求。

当缩放更改时,是否有任何方法可以中止待处理的地图图块请求?如果不通过 OpenLayers,是否有某种方法可以中止对特定 URL 的所有待处理请求?

I have an OpenLayers map with a TMS layer. For each zoom change, my map makes about 56 requests for map tiles (depending on the map/screen size). If the user rapidly zooms in or out, all these map tile requests get queued up in order, and my understanding is that the map tile requests from OpenLayers are not called asynchronously. So if a user zooms in or out rapidly, there ends up being many requests for map tiles that are no longer needed, and the ones that are needed will not load in until all previous requests are made.

Is there any way to abort pending map tile requests when the zoom changes? If not through OpenLayers, is there some way to abort all pending requests to a specific URL?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

逆光飞翔i 2024-12-28 18:52:49

更新到 OpenLayers 2.12 或更高版本,因为自此版本以来,图块队列已得到增强,现在将避免旧请求。引用 OpenLayers 2.12 发行说明

平铺代码已被彻底修改,因此网格层中的平铺加载是
现在在队列中完成。磁贴队列提供了对磁贴的更多控制
发送到服务器的请求。未处理的瓷砖请求
避免了更多需要(例如,在缩放或平移之后),这
提高性能并减少服务器负载。

Update to OpenLayers 2.12 or above, because since this version the tile queue has been enhanced and will now avoid old requests. To cite from the OpenLayers 2.12 Release Notes:

The tiling code has been overhauled so tile loading in grid layers is
now done in a queue. The tile queue gives more control on the tile
requests sent to the server. Pending requests for tiles that are not
needed any more (e.g. after zooming or panning) are avoided, which
increases performance and reduces server load.

思慕 2024-12-28 18:52:49

当您通过鼠标滚轮定义缩放时,可以使用以下代码来防止加载所有中间缩放级别的图块:

new OpenLayers.Control.Navigation({
    "zoomWheelEnabled": true,
    "mouseWheelOptions": {
        "interval": 250, 
        "cumulative": true
    }
})

When you have defined zooming by mousewheel, you can use the following code to prevent loading tiles for all intermediate zoomlevels:

new OpenLayers.Control.Navigation({
    "zoomWheelEnabled": true,
    "mouseWheelOptions": {
        "interval": 250, 
        "cumulative": true
    }
})
微暖i 2024-12-28 18:52:49

对于每次缩放更改,我的地图都会发出大约 56 个地图图块请求(取决于地图/屏幕尺寸)。如果用户快速放大或缩小,所有这些地图图块请求都会排队

您可以按照 janb 的建议避免用户经过的每个缩放级别的请求(他们的代码在最新版本中不再工作)。

默认情况下,MouseWheelZoom 在第一次滚动事件后等待 80 毫秒,然后再执行任何操作 (timeout:80),并且不允许一次缩放多次单击 (maxDelta:1) )(文档没有说明为什么要设置这些减速带)。当它等待超时时,不会发生缩放,因此不会加载中间缩放级别。 通过将 maxDelta 设置为更大的值,如果用户在超时期限内滚动超过一次点击,则可以跳过缩放级别。

您可以通过减少80 毫秒,但最终您放弃了足够快的缩放速度以跳过加载缩放级别的机会。

我发现相关的另一个设置是动画持续时间。它堆叠动画而不是等待它们完成,因此(值得庆幸的是)这不会限制用户(许多应用程序让用户在启用交互之前等待动画)。尝试将其设置为 2500 毫秒,然后在动画仍在播放时滚动到地图上的不同点:)。默认值为 250 毫秒,但我发现稍微低一点的值会感觉更灵敏。

根据一些测试,6 个缩放级别大约是我能做到的最精确的缩放级别,而不会出现在我不想要的地方。这对每个人来说可能不是一个好的默认值,但 maxDelta:6 对我来说似乎是一个很好的值。将超时设置为 30 毫秒会导致它无法累积我一次尝试执行的所有缩放操作,因此 40 毫秒对我来说在执行用户要求的操作和避免不必要的等待之间取得了良好的平衡。

let map = new ol.Map({
    interactions: ol.interaction.defaults.defaults({
        mouseWheelZoom: false,
    }).extend([
        new ol.interaction.MouseWheelZoom({
            maxDelta: 6,
            timeout: 40,
            duration: 150,
        }),
    ]),
});

代码看起来有点笨拙,但这就是示例 覆盖默认的 MouseWheelZoom 选项。

For each zoom change, my map makes about 56 requests for map tiles (depending on the map/screen size). If the user rapidly zooms in or out, all these map tile requests get queued up

You can avoid requests for each zoom level that the user passes through as suggested by janb (their code does not work in the latest release anymore).

By default, MouseWheelZoom waits 80 milliseconds after the first scroll event before doing anything (timeout:80) and does not allow zooming more than one click at a time (maxDelta:1) (the documentation doesn't say why these speed bumps are in place). While it waits for the timeout, no zooming happens and so no intermediate zoom levels are loaded. By setting maxDelta to a greater value, the user can skip over zoom levels if they scroll more than one click within the timeout period.

You could make the application more responsive by reducing the 80 milliseconds, but eventually you forego the chance of zooming fast enough to skip loading zoom levels.

Another setting I found relevant is the animation duration. It stacks animations instead of waiting for them to complete, so this (thankfully) does not limit the user (so many applications make the user wait for an animation before interaction is enabled). Try setting it to 2500 ms and then scroll at different points on the map while the animation is still going :). The default is 250 ms but I find a slightly lower value to feel more responsive.

From some testing, 6 zoom levels is about the most precise I can do without ending up in a place I didn't intend to. This may not be a good default for everyone, but maxDelta:6 seems to be a good value for me. Setting a timeout of 30 ms resulted in it not accumulating all the zooming I tried to do at once, so 40 ms strikes a good balance for me between doing what the user asked and no unnecessary waiting.

let map = new ol.Map({
    interactions: ol.interaction.defaults.defaults({
        mouseWheelZoom: false,
    }).extend([
        new ol.interaction.MouseWheelZoom({
            maxDelta: 6,
            timeout: 40,
            duration: 150,
        }),
    ]),
});

The code seems a bit unwieldy but this is how an example overrides the default MouseWheelZoom options.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文