首页 > 解决方案 > HERE 映射 JS,禁用鼠标缩放但不平移

问题描述

我希望用户能够用鼠标/手指平移地图。但是我想将缩放限制为仅使用 HERE ui 控件。

我尝试了以下方法:

    // Since our marker is in the center of the map we need to make sure zooming occurs at center of map only
    // The user can zoom to their mouse position which may not be center, so we need to disable and allow zooming
    // via the +/- buttons only :(

    this.mapBehavior.disable(window.H.mapevents.Behavior.WHEELZOOM)
    this.mapBehavior.disable(window.H.mapevents.Behavior.PINCH_ZOOM)
    this.mapBehavior.disable(window.H.mapevents.Behavior.DBL_TAP_ZOOM)
    this.mapBehavior.enable(window.H.mapevents.Behavior.PANNING) <-- this renables zoom

不幸的是,如果我禁用 WHEELZOOM,我也会失去平移的能力。如果我重新启用平移,缩放将重新打开。

如何在不禁用平移的情况下禁用缩放?

标签: javascripthere-api

解决方案


PINCH_ZOOM禁用拖动的问题是您尝试禁用 Behavior 对象 ( & DBL_TAP_ZOOM)中不存在的功能。此外,在 API 3.0 版和 API 3.1 版(最新)中禁用功能的方式也不同。从上面的代码中,我看到您使用的是旧版本 3.0,因此:

在 3.0 版中,您只能禁用/启用 3 个功能: H.mapevents.Behavior.DBLTAPZOOMH.mapevents.Behavior.DRAGGINGH.mapevents.Behavior.WHEELZOOM

因此,禁用缩放的代码应如下所示:

this.mapBehavior.disable(window.H.mapevents.Behavior.WHEELZOOM)
this.mapBehavior.disable(window.H.mapevents.Behavior.DBLTAPZOOM)

在 3.1 版中,您可以禁用/启用更多功能: H.mapevents.Behavior.Feature.PANNING , H.mapevents.Behavior.Feature.PINCH_ZOOM, H.mapevents.Behavior.Feature.WHEEL_ZOOM, H.mapevents.Behavior.Feature.DBL_TAP_ZOOM, H.mapevents.Behavior.Feature.FRACTIONAL_ZOOM, H.mapevents.Behavior.Feature.HEADING, H.mapevents.Behavior.Feature.TILT,

因此,禁用缩放的代码(如果您使用 3.1 版)如下所示:

this.mapBehavior.disable(window.H.mapevents.Behavior.Feature.WHEEL_ZOOM)
this.mapBehavior.disable(window.H.mapevents.Behavior.Feature.PINCH_ZOOM)
this.mapBehavior.disable(window.H.mapevents.Behavior.Feature.DBL_TAP_ZOOM)

请参阅简单的jsfiddle示例以禁用 API 版本 3.1 的缩放。


推荐阅读