leaflet - How to use a unique PNG for multiple tile?
问题描述
I want to use an offline map with Leaflet so I use PNG for my tiles. I want to use a unique image for the water so I don't need to have a lot of identical PNG.
The files are stored this way myMap/z/x/y.png With z the level of zoom, x and y the coordinate.
I think I can easily get the z, x and y of all the water tiles. I just need to know if it's possible to tell Leaflet that if a tile with the same z, x and y than one in the array (or map or something else), it display myMap/water.png instead.
解决方案
This is easy to do if you understand how custom tilelayers in Leaflet work. For this particular case you'll want a getTileUrl
method that returns the same tile when some condition is met, e.g. something like:
L.TileLayer.RepeatedWater = L.TileLayer.extend({
getTileUrl: function(coords) {
if ( checkIfTheseCoordsAreInTheWater(coords) ) {
return "/tiles/water.png";
} else {
return L.TileLayer.prototype.getTileUrl.call(this, coords);
}
}
});
Be aware that other approaches exists. In particular, a service worker to intercept all network requests to the tiles, check if their URL is not out of bounds or not available (or whatever) and return a cached response from a "default water" tile in that case.
推荐阅读
- javascript - 我应该如何将变量从我的外部路由异步传递到 Node.js 中的 javascript 控制器?
- java - 通过 REST 控制器中的 Url 将空 ID 获取到后端
- c# - 我的委托永远不会在 .net 核心中触发
- php - 从 JSON URL 获取 DOI 值列表
- android - 如何在没有 ffmpeg 的 node.js 中编码 h.264
- amazon-web-services - Dynamo Stream Lambda - 每分钟只读取新记录
- java - 静态方法为所有变量返回值
- python - 如何使用 MIMEText 对“发件人姓名”进行编码
- python - 如何将 opencv Mat 类型传递给 Python 并返回一个数组?
- python - 查找 Dataframe 值并返回列号