简体   繁体   English

如何在打开图层中创建新的缩放按钮?

[英]How to create new zoom buttons in Open Layers?

I have created two buttons (using Bootstrap) for zooming in and out:我创建了两个按钮(使用 Bootstrap)用于放大和缩小:

<div class="btn-toolbar" id="buttontoolbar">
    <div id="newSim", class="btn-group-vertical">
        <button type="button" title="Zoom in", class="btn btn-default",id="olZoomOutLink"><img src="plus.svg"></button>
        <button type="button" title="Zoom out", class="btn btn-default", id="customZoomOut"><img src="minus.svg"></button>
    </div>
</div>

I have also turned off the default zoom buttons for Open Layers:我还关闭了打开图层的默认缩放按钮:

var map = new ol.Map({
    controls: ol.control.defaults({
        zoom: false,
    })
})

Now my question is, how can I make my two buttons work for zoom-in and zoom-out?现在我的问题是,如何让我的两个按钮用于放大和缩小?

Thanks!谢谢!

After creating your html buttons in the html file, then you add a control on each one by referring the id from the html.在 html 文件中创建 html 按钮后,然后通过引用 html 中的 id 在每个按钮上添加一个控件。 After that listen to the click event on the map.之后收听地图上的点击事件。

 var zoomType; document.getElementById('customZoomOut').onclick = function() { zoomType="customZoomOut"; }; document.getElementById('olZoomOutLink').onclick = function() { zoomType="olZoomOutLink"; }; var map = new ol.Map({ target: 'map', layers: [new ol.layer.Tile({ source: new ol.source.OSM() })], view: new ol.View({ center: [0, 0], zoom: 2 }) }); map.on('click', function(evt) { if(zoomType=="olZoomOutLink"){ var view = map.getView(); var zoom = view.getZoom(); view.setZoom(zoom - 1); } if(zoomType=="customZoomOut"){ var view = map.getView(); var zoom = view.getZoom(); view.setZoom(zoom + 1); } });
 <link rel="stylesheet" href="https://openlayers.org/en/v4.6.4/css/ol.css" type="text/css"> <!-- The line below is only needed for old environments like Internet Explorer and Android 4.x --> <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script> <script src="https://openlayers.org/en/v4.6.4/build/ol.js"></script> <div class="btn-toolbar" id="buttontoolbar"> <div id="newSim" , class="btn-group-vertical"> <button type="button" title="Zoom in" class="btn btn-default" id="olZoomOutLink"><img src="plus.svg"></button> <button type="button" title="Zoom out" class="btn btn-default" id="customZoomOut"><img src="minus.svg"></button> </div> </div>

An addition to the newer versions of OL and with animations .新版本的 OL 和动画的补充。 The behavoir of the default buttons are different and smoother.默认按钮的行为不同且更流畅。 So this is a solution without setZoom() .所以这是一个没有setZoom()的解决方案。 It is a bit generic, so adjust to your framework它有点通用,因此请根据您的框架进行调整

<button onclick="zoomTo(+1)">+</button> 
<button onclick="zoomTo(-1)">-</button> 

And with this function有了这个功能

zoomTo(amount){
    const view = map.getView();
    const zoom = view.getZoom();
    view.animate({ zoom: zoom + amount})
}

To disable the normal controls, when defining map:要禁用法线控件,请在定义地图时:

new Map({
    ...
    controls: []
})

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

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