如何为OpenStreatMap超时添加自定义传单错误处理程序?

osh3o9ms  于 2021-09-13  发布在  Java
关注(0)|答案(1)|浏览(349)

有几个小时,我发现超时错误,部分Map是蓝色的。

也看到
openstreetmap经常发送网关超时错误
https://forum.openstreetmap.org/viewtopic.php?id=68026
这个问题是由于某些osm服务器而不是我的代码造成的。
处理(暂时)问题的推荐方法是什么?我想
显示警告,供用户通知暂时的osm问题
将Map切换到其他层或其他osm服务器。
手动检查可用性的方法可以是获取一些平铺图像,如https://tile.openstreetmap.org/8/136/85.png 并检查需要多长时间才能拿到。
传单是否已经为层错误处理提供了一些内置解决方案/api功能?

8hhllhi2

8hhllhi21#

A.有一个后备插件:https://github.com/ghybs/leaflet.tilelayer.fallback
B有关手动处理磁贴错误的信息,请参见此处:
https://gis.stackexchange.com/questions/347646/specify-an-alternative-url-for-a-tilelayer-to-use-in-leaflet
当发生平铺加载错误时,传单触发平铺错误事件。在使用事件处理函数的情况下,事件参数具有属性tile和coords,可用于从辅助url启动tile加载。

var url1 = 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
var url2 = 'http://{s}.tile.osm.org/{z}/{x}/{y}.png';
var url2s = 'abc';

var map = L.map('map').setView([37.8, -96], 4);

var myLayer = L.tileLayer(url1, {maxZoom: 18}).addTo(map);

function handleTileError(evt) {
  if (evt.tile._hasError) return;

  var si = Math.floor((Math.random() * 3));
  var tileSrc = url2.replace(/{s}/g, url2s.substring(si, si + 1));
  tileSrc = tileSrc.replace(/{x}/g, evt.coords.x);
  tileSrc = tileSrc.replace(/{y}/g, evt.coords.y);
  tileSrc = tileSrc.replace(/{z}/g, evt.coords.z);
  evt.tile._hasError = true;
  evt.tile.src = tileSrc;
};

myLayer.on('tileerror', handleTileError);

相关问题