简体   繁体   English

Openlayers 3替换失败的图块

[英]Openlayers 3 Replace a failed tile

We are running Open Layers 3.15. 我们正在运行开放层3.15。 Sometimes we get a dropped or failed tile. 有时我们会掉落或失败。 Currently it displays nothing, (which can be confusing for our users) so we'd like to replace this with a tile that says 'no data' or something. 目前,它什么也没有显示(这可能会使我们的用户感到困惑),因此我们希望将其替换为显示“无数据”之类的图块。 I've tried picking up the event and replacing the source of the tile eg 我试过捡起事件并替换磁贴的来源,例如

            source.on('tileloaderror', function(){
               source.setUrl('./images/map/failureTile.png');
            });

but the problem with this is, instead of doing this on 1 tile, it does it for the entire layer, we don't want that. 但是这样做的问题是,不是在一个图块上执行此操作,而是在整个图层上执行此操作,我们不希望这样做。

Anyone know how we can do this for just the tile that failed and not the entire layer? 有谁知道我们如何仅针对失败的图块而不是整个图层执行此操作?

It's 2018, but for someone who may need this. 现在是2018年,但对于可能需要此服务的人来说。 Tested on v5.3.0 在v5.3.0上测试

source.on('tileloaderror', function (event) {
  var tileLoadFunction = function (imageTile, src) {
    imageTile.getImage().src = './images/map/failureTile.png';
  };
  if (event.tile.tileLoadFunction_ != tileLoadFunction) {
    event.tile.tileLoadFunction_ = tileLoadFunction;
    event.tile.load();
  }
});

This code relys on private function event.tile.tileLoadFunction_ is exposed. 此代码依赖于私有函数event.tile.tileLoadFunction_被公开。

Unfortunately xnakos' answer doesn't work on v5.3.0 because the event.tile.getImage() has been replaced with 1x1 canvas image by internal error handler. 不幸的是,xnakos的答案在v5.3.0上不起作用,因为event.tile.getImage()已由内部错误处理程序替换为1x1画布图像。

Also noted, changing event.tile.src_ directly, seems to be an option, but it doesn't work either because of cache key or something. 还应注意,直接更改event.tile.src_似乎是一种选择,但由于缓存键或其他原因而无法使用。

A tile which failed to load should have a distinct class (.olImageLoadError). 未能加载的图块应具有不同的类(.olImageLoadError)。 You can define a CSS rule not show these items. 您可以定义不显示这些项目的CSS规则。

.olImageLoadError { 
    display: none !important;
}

You could try this: 您可以尝试以下方法:

source.on('tileloaderror', function(event) {
    event.tile.getImage().src = './images/map/failureTile.png';
});

You need the event parameter, that can get you the tile that failed, so that you can change the tile's image. 您需要event参数,该参数可以使失败的磁贴获取,以便您可以更改磁贴的图像。

Warning: I tested the code above using tileloadend instead of tileloaderror , because my tiles never fail on me. 警告:我使用tileloadend而不是tileloaderror测试了上面的代码,因为我的图块永远不会对我失败。 :) I used a simulated failure rate using Math.random() and some random tiles were replaced by the image specified. :)我使用Math.random()来模拟失败率,并且某些随机图块被指定的图像替换。 I cannot think of a reason the code above would not work. 我无法想到上述代码无法正常工作的原因。 If you verify it works, I will remove this warning from my answer. 如果您确认它有效,我将从我的答案中删除此警告。 I tested it on OpenLayers 3.14.2 and on an OSM source. 我在OpenLayers 3.14.2和OSM源上对其进行了测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM