当您在 map 上使用鼠标滚轮时,它会放大或缩小,就像我们在 Bing map 或 Google map 上看到的那样。我看到每次滚动时缩放系数都是 1.2。我被要求更改它,因为它缩放得太远或太近。那有可能吗?
我尝试使用 MouseWheel 事件并通过使用“e.Handled = true;”禁用它来自行管理缩放。它以某种方式起作用,但我失去了它附带的动画,而且缩放指向 map 的当前中心而不是鼠标光标。
即使说这是不可能的,我们也将不胜感激。
谢谢
请您参考如下方法:
这是当前实现正在做的事情:
在 OnMouseWheel 中调用以下方法:
this.ZoomAboutViewportPoint(((double) e.Delta) / 100.0, e.GetPosition(this));
它的实现是:
private void ZoomAboutViewportPoint(double zoomLevelIncrement, Point zoomTargetInViewport)
{
base.ZoomAndRotateOrigin = new Point?(zoomTargetInViewport);
base.ViewBeingSetByUserInput = true;
base.SetView((double) (base.TargetZoomLevel + zoomLevelIncrement), base.TargetHeading);
base.ViewBeingSetByUserInput = false;
}
显然,您不能直接设置 base.ViewBeingSetByUserInput 和 base.ZoomAndRotateOrigin,因为它们是内部的。
不过,您可以根据视口(viewport)坐标只使用 SetView,但仍然会丢失漂亮的动画部分。
或者,您可以通过反射设置上述值,但这是一个脆弱的 hack,如果控件发生变化,很容易崩溃。
--- 更新
如上所述:如果您连接到 MouseWheel 事件,这里是通过私有(private)方法的反射调用使其工作的代码:
void BingMap_MouseWheel(object sender, MouseWheelEventArgs e)
{
e.Handled = true;
System.Reflection.MethodInfo dynMethod = this.BingMap.GetType().GetMethod("ZoomAboutViewportPoint", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
dynMethod.Invoke(this.BingMap, new object[] { (((double)e.Delta) / 400d), e.GetPosition(this.BingMap) });
}
这使它的速度降低了四分之一,并使它可以用最少的代码使用。但这又是一次黑客攻击!
