OpenLayers 中的最小/最大缩放级别
我正在使用 OpenLayers 在网页中显示地图。我使用的是 CloudMade 中的图块,但 OpenStreetMap 中的 Mapnik 图块也会出现同样的问题。
我试图限制地图,以便用户无法一直缩放到世界视图 - 我希望它们至少保持在大约城市级别的缩放。
我尝试使用 minZoomLevel
/ maxZoomLevel
/ numZoomLevels
属性;只有 maxZoomLevel
和 numZoomLevels
属性似乎有效,而 minZoomLevel
属性似乎被完全忽略。
还有其他方法可以实现此目的吗?
I'm using OpenLayers to display a map in a web page. I am using tiles from CloudMade, but the same issues occur with the Mapnik tiles from OpenStreetMap.
I'm trying to restrict the map so that the user cannot zoom all the way out to world view -- I want them to stay at roughly a city level zoom at minimum.
I have tried using the minZoomLevel
/ maxZoomLevel
/ numZoomLevels
properties; only the maxZoomLevel
and numZoomLevels
properties seem to work, whereas the minZoomLevel
property seems to be completely ignored.
Is there another way to accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您可以重写 isValidZoomLevel 函数。像这样的东西(未经测试):
编辑
或者您需要覆盖可用的分辨率,请参阅此处的示例:http://dev.openlayers.org/examples/zoomLevels.html
You could override the
isValidZoomLevel
function. Something like this (not tested):EDIT
Or you would need to override the available resolutions, see the example here: http://dev.openlayers.org/examples/zoomLevels.html
XYZ 图层不支持 minZoomLevel,OSM 是该图层的子类。
请参阅以下票证了解解决方法:
minZoomLevel is not supported for XYZ layers, of which OSM is a subclass.
See the following tickets for workarounds:
我发现限制 XYZ 图层的 maxZoom 级别的最简单方法是重写 getNumZoomLevels 方法:
这会导致缩放超过级别 10,并且还会正确绘制 ZoomBar 控件。这个和zoomOffset选项的组合应该允许您轻松控制最小/最大缩放,而不必弄乱分辨率或子类。
I found the simplest way to restrict the maxZoom levels for a XYZ layer was to override the getNumZoomLevels method:
This pevents zooming beyond level 10 and also draws the zoomBar control correctly. This and a combination of the
zoomOffset
option should allow you to easily control min/max zoom without having to mess around with resolutions or subclasses.这里的其他答案参考版本 3 之前的 OpenLayers 版本。
使用 OpenLayers 3,您可以在初始化地图时使用
maxZoom
地图选项,如下所示:更多信息可以在 OpenLayers 文档中找到:http://openlayers.org/en/v3.0.0/doc/tutorials/concepts。 html
Other answers here refer to OpenLayers versions prior to version 3.
With OpenLayers 3, you can use the
maxZoom
map option when initializing the map, as follows:More information can be found in OpenLayers documentation: http://openlayers.org/en/v3.0.0/doc/tutorials/concepts.html
现在我在“视图”中使用它:
它工作正常
Now i use this in the "View":
And it works fine
定义分辨率并不能解决问题(即使在 v2.1 中) - 我没有找到任何永久修复,所以我在 JS 中制作了自己的缩放栏 - 这就是我向任何使用自定义缩放栏遇到问题的人推荐的瓷砖。
Defining resolutions doesn't solve the problem (even in v2.1) - I didn't find any pernament fix for this, so I made my own zoombar in JS - that's what I would recommend to anyone who encounters issues with zoombar using custom tiles.
我最终得到了这个解决方案,因为我无法弄清楚如何在我的设置中使用 ZoomOffset。
I ended up with this solution as i could not figure out how to use zoomOffset in my setup.
我尝试过这样的事情,它对我有用:
I tried something like this and it works for me:
我找到的最佳答案(包括示例)位于 OpenLayers - 限制缩放级别的数量。我这里就不贴了,请看那边的说明。
The best answer I found (including an example) was at OpenLayers - limit the number of zoom levels. I don't paste it here, please see the instructions over there.