【发布时间】:2022-01-25 16:43:08
【问题描述】:
我尝试使用leaflet.js 允许用户在我的网站上缩放和平移大型 SVG 文件。我使用以下script 来显示带有传单的 SVG:
// Using leaflet.js to pan and zoom a big image.
// See also: http://kempe.net/blog/2014/06/14/leaflet-pan-zoom-image.html
var factor = 1;
// create the slippy map
var map = L.map('image-map', {
minZoom: 1,
maxZoom: 5,
center: [0, 0],
zoom: 1,
crs: L.CRS.Simple
});
function getMeta(url) {
const img = new Image();
img.addEventListener("load", function () {
var w = this.naturalWidth;
var h = this.naturalHeight;
var southWest = map.unproject([0, h], map.getMaxZoom() - 1);
var northEast = map.unproject([w, 0], map.getMaxZoom() - 1);
var bounds = new L.LatLngBounds(southWest, northEast);
// add the image overlay,
// so that it covers the entire map
L.imageOverlay(img.src, bounds).addTo(map);
// tell leaflet that the map is exactly as big as the image
map.setMaxBounds(bounds);
map.fitBounds(bounds); // test
});
img.src = url;
}
getMeta("/assets/images/bhikkhu-patimokkha.svg")
您可以看到结果here。问题是它在我的 iPhone 上运行良好,缩放级别合适,很容易放大,但在桌面版本上,你只能缩小不能放大。
我尝试将 minZoom: 1 更改为不同的值,但它似乎没有做任何事情。我能够让它在桌面上工作的唯一方法是在 var w 和 var h 上添加一个乘数 10,但它会在移动版本上搞砸.
我在使用 PNG 图像方面取得了更多成功,但它们的加载速度非常慢。也许代码不适合 SVG,尤其是在调用 naturalHeight 时?但是当我调试它时它看起来很好。
感谢您的帮助。
如果你愿意,这里有一个codepen。
编辑:使用来自@Grzegorz T 的更好的代码。它现在在桌面上运行良好。但在 Safari iOS 上,它被过度缩放并且无法取消缩放......见下图(它在 Iphone 上使用之前的代码,但在 Destop 上没有......)
【问题讨论】:
-
尝试一个负数,例如
minZoom: -4我知道很奇怪,但它对我有用 - image-instead-of-map -
@Grzegorz T. 谢谢你,正如你提到的,我尝试了不同的值。到目前为止 min-zoom = 1 和 max-zoom = 0 给出了一个有趣的结果,但也不能放大。负最小缩放似乎不起作用。我现在已经制作了一个代码笔来解决我的问题。
标签: svg mobile leaflet zooming