使用 AJAX 控件 v7 限制 Bing 地图上的最小/最大缩放?
我正在开发一个使用 Bing 地图 AJAX 控件 v7 的网站。我需要做的一件事是限制缩放级别,以防止用户放大超过某个级别或缩小超过某个级别。
我在 Map 对象上发现了一个“getZoomRange”方法,在检查它之后,它只是返回一个具有“min”和“max”属性的对象文字。所以,我认为重载它可能会起到作用:
// "map" is our Bing Maps object
map.getZoomRange = function ()
{
return {
max: 14
min: 5
};
};
……但是不行。它没有任何效果(它实际上与使用默认仪表板时缩放滑块的外观有关)。
劫持事件并阻止其继续似乎也没有效果。
I'm working on a site that makes use of v7 of the Bing Maps AJAX Control. One of the things I need to do is restrict the zoom level so as to prevent users from zoom in past a certain level, or zoom out past a certain level.
I found a "getZoomRange" method on the Map object, after inspecting it, it simply returns an object literal with "min" and "max" properties. So, I figured overloading it would probably do the trick:
// "map" is our Bing Maps object
map.getZoomRange = function ()
{
return {
max: 14
min: 5
};
};
...but no. It has no effect (it actually has something to do with the appearance of the zoom slider when using the default Dashboard).
Hijacking the event and preventing it from proceeding also seems to have no effect.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据 Bing 地图支持,执行此操作的唯一方法(这不是特别优雅,并且会导致地图上出现一些不受欢迎的抖动)如下:
According to Bing Maps support, the only way to do this (which isn't particularly elegant, and results in some unwelcome jitter on the map) is as follows:
我正在处理一个类似的问题,最终我做了一些与 MrJamin 在他的回答中描述的非常相似的事情,有一个(微妙但主要的)区别:我为
targetviewchanged
添加了一个处理程序。根据 MSDN 上的官方文档,'targetviewchanged ' 当地图导航的视图发生变化时会发生
。另外,我没有调用 Map#getZoom,而是使用了 Map#getTargetZoom,它返回地图导航到的视图的缩放级别。请注意,这种方法可以防止抖动。这是我的代码的缩短版本:
I was dealing with a similar issue and I ended up doing something very similar to what MrJamin describes in his answer, with one (subtle, but major) difference: I added a handler for
targetviewchanged
. According to the official docs on MSDN,'targetviewchanged' occurs when the view towards which the map is navigating changes
. Also, instead of callingMap#getZoom
, I usedMap#getTargetZoom
whichreturns the zoom level of the view to which the map is navigating
. Note, this approach prevents jitter.Here's the shortened version of my code:
实现此目的的另一种方法是处理鼠标滚轮移动时引发的事件。 http://msdn.microsoft.com/en-us/library/gg427609.aspx
当你处理
mousewheel
事件时,你可以检查鼠标滚轮是否向前或向后滚动,然后按顺序检查map.targetZoom()
与最小或最大缩放值进行比较。如果超出最小值或最大值,则设置event.handled = true
。这可以防止该事件被任何其他处理程序处理,从而防止默认行为。从文档中:见下文:
Another way to achieve this is to handle the event thrown when the mouse wheel is moved. http://msdn.microsoft.com/en-us/library/gg427609.aspx
When you handle the
mousewheel
event, you can check whether the mouse wheel is being scrolled forwards or backwards, and then check themap.targetZoom()
in order to compare with a min or max zoom value. If the min or max are exceeded, then setevent.handled = true
. This prevents the event from being handled by any other handlers which prevents default behaviour. From the documentation:See below: