简体   繁体   English

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

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

I use OpenLayers and want to create another navigation-control in the upper-left side.我使用 OpenLayers 并想在左上角创建另一个导航控件。 I know how to add Controls, but this navigation is added at default while creating the OpenLayers-Map.我知道如何添加控件,但在创建 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.Control.PanZoom。

The map object has a property called controls that is an array of OpenLayers.Control objects.地图对象有一个名为controls的属性,它是一个OpenLayers.Control对象数组。 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() .如果未明确设置此属性,则 OpenLayers 将假设您需要默认控件集,包括OpenLayers.Control.Navigation()OpenLayers.Control.PanZoom()OpenLayers.Control.ArgParser()OpenLayers.Control.Attribution()

To remove PanZoom or any other default control, simply set the controls property array at the time you construct the Map object.要删除PanZoom或任何其他默认控件,只需在构造Map对象时设置controls属性数组即可。 Here is a code example:这是一个代码示例:

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 note that by setting the controls property that you will not get any Control objects be default.请注意,通过设置controls属性,您将不会获得任何默认的Control对象。 Any controls you need must be added manually.您需要的任何控件都必须手动添加。

Here is a link to the source code of the Map object if you want to see how it works for yourself.如果您想亲自了解它是如何工作的,这里有一个指向Map对象源代码的链接。

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);

I would have expected map.removeControl(OpenLayers.Control.PanZoom) to work but apparently not.我本来希望map.removeControl(OpenLayers.Control.PanZoom)可以工作,但显然不行。

The other answers are out of date in relation to the current verison of OpenLayers which is 7 at time of writing (Dec 2022).其他答案相对于 OpenLayers 的当前版本已过时,在撰写本文时(2022 年 12 月)为 7。 Here is an example of how to just at the Zoom control in manually, rather than using the defaults:下面是一个示例,说明如何仅在缩放控件中手动设置,而不是使用默认值:

 import Map from 'ol/Map'; import Zoom from 'ol/control/Zoom'; import { Tile as TileLayer } from 'ol/layer'; import View from 'ol/View'; const map = new Map({ layers: [ new TileLayer({ source: new OSM(), }), ], target: 'map', view: new View({ center: [0, 0], zoom }), controls: [new Zoom()] });

You can check out this official example to show extending from the defaults:您可以查看此官方示例以显示从默认值扩展:

https://openlayers.org/en/latest/examples/navigation-controls.html https://openlayers.org/en/latest/examples/navigation-controls.html

If you want an example of adding a control to the defaults dynamically this is a good example of that behaviour:如果您想要一个动态添加控件到默认值的示例,这是该行为的一个很好的示例:

https://openlayers.org/en/latest/examples/scaleline-indiana-east.html https://openlayers.org/en/latest/examples/scaleline-indiana-east.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM