Javascript 如何删除 OpenLayers-Map 中的标准控件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3441620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:38:05  来源:igfitidea点击:

How can I remove standard controls in an OpenLayers-Map?

javascriptopenlayers

提问by Mnementh

I use OpenLayers and want to create another navigation-control in the upper-left side. I know how to add Controls, but this navigation is added at default while creating the OpenLayers-Map. So I want to remove that Control, to add an own. I know already, that the default-control is an OpenLayers.Control.PanZoom.

我使用 OpenLayers 并想在左上角创建另一个导航控件。我知道如何添加控件,但是在创建 OpenLayers-Map 时默认添加了此导航。所以我想删除那个控件,添加一个自己的。我已经知道,默认控件是 OpenLayers.Control.PanZoom。

回答by atogle

The map object has a property called controlsthat is an array of OpenLayers.Controlobjects. If this property is not explicitly set then OpenLayers will assume that you want the default control set, including OpenLayers.Control.Navigation(), OpenLayers.Control.PanZoom(), OpenLayers.Control.ArgParser(), and OpenLayers.Control.Attribution().

地图对象有一个称为对象controls数组的属性OpenLayers.Control。如果没有明确设置该属性,然后将的OpenLayers假设您想要默认控件集,包括OpenLayers.Control.Navigation()OpenLayers.Control.PanZoom()OpenLayers.Control.ArgParser(),和OpenLayers.Control.Attribution()

To remove PanZoomor any other default control, simply set the controlsproperty array at the time you construct the Mapobject. Here is a code example:

要删除PanZoom或任何其他默认控件,只需controls在构造Map对象时设置属性数组。这是一个代码示例:

var map = new OpenLayers.Map('map', {
    controls: [
        new OpenLayers.Control.Navigation(),
        new OpenLayers.Control.ArgParser(),
        new OpenLayers.Control.Attribution()
    ]
});

Here is a live example.

这是一个活生生的例子

Please notethat by setting the controlsproperty that you will not get any Controlobjects be default. Any controls you need must be added manually.

请注意,通过设置controls您不会获得任何Control对象的属性是默认的。您需要的任何控件都必须手动添加。

Here is a link to the source code of the Mapobjectif you want to see how it works for yourself.

如果您想了解它是如何为自己工作的,这里有一个指向Map对象源代码的链接。

回答by Leif Gruenwoldt

I would have expected map.removeControl(OpenLayers.Control.PanZoom)to work but apparently not.

我本来希望map.removeControl(OpenLayers.Control.PanZoom)工作,但显然没有。

回答by Prosenjit Saha

Traverse the array of controls and then remove the zoom control

遍历控件数组,然后移除缩放控件

map.getControls().forEach(function(control) {
  if (control instanceof ol.control.Zoom) {
    map.removeControl(control);
  }
}, this);