简体   繁体   English

如何在openlayers API中定义缩放按钮?

[英]How to define zoom button in openlayers API?

How can I remove default ol-zoom buttons and define another zoom buttons with different tags and class names in openlayers API? 如何在openlayers API中删除默认的ol-zoom按钮并用不同的标签和类名定义另一个缩放按钮?

<div class="ol-zoom ol-unselectable ol-control">
    <button class="ol-zoom-in" type="button" title="Zoom in">+</button>
    <button class="ol-zoom-out" type="button" title="Zoom out">−</button>
</div>

I used this code to set CSS class name for zoom buttons but I dont know why it doesn't work : 我使用以下代码来设置缩放按钮的CSS类名称,但我不知道为什么它不起作用:

new ol.control.Zoom({
          className: "my-new-class-name"
        });

I cannot see if you actually added the Zoom control to the map. 我看不到您是否确实在地图上添加了“缩放”控件。 Instantiating the control itself does not change the OL map. 实例化控件本身不会更改OL映射。 Also, the map has a default zoom control instance. 此外,地图具有默认的缩放控件实例。 If you called addControl on the map, you might have ended up with two zoom controls being placed above each other. 如果在地图上调用addControl ,则可能最终将两个缩放控件放置在彼此上方。

You can give your control as a option to the map initialization. 您可以将控件作为地图初始化的选项。 If you do not want to list all other controls manually, just use ol.control.defaults with the option zoom: false . 如果您不想手动列出所有其他控件,只需将ol.control.defaultszoom: false选项一起使用。 It omits the zoom control but adds the default attribution and rotation controls. 它省略了缩放控件,但添加了默认的归因和旋转控件。

You could even give your options directly to the ol.control.defaults: 您甚至可以将选项直接提供给ol.control.defaults:

ol.control.defaults({
    zoomOptions: {
        className: "ol-zoom my-new-class-name"
    }
})

Secondly, setting the className is not additive, it replaces the default ol-zoom class. 其次,将className设置为非加性的,它将替换默认的ol-zoom类。 So your control might be added to the DOM, but has no styling and is placed offscreen. 因此,您的控件可能已添加到DOM,但没有样式,因此不在屏幕上。 You can fix that by adding ol-zoom to the className too. 您也可以通过将ol-zoom添加到className来解决此问题。

Here is a working example: 这是一个工作示例:

 var map = new ol.Map({ controls: ol.control.defaults({ zoom: false }).extend([ new ol.control.Zoom({ className: "ol-zoom my-new-class-name" }) ]), layers: [ new ol.layer.Tile({ source: new ol.source.OSM() }) ], target: 'map', view: new ol.View({ center: [0, 0], zoom: 3, }) }); 
 .ol-zoom.my-new-class-name { background: red; } 
 <link href="https://openlayers.org/en/v4.6.4/css/ol.css" rel="stylesheet"/> <script src="https://openlayers.org/en/v4.6.4/build/ol-debug.js"></script> <div id="map" class="map"></div> 

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

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