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

时间:2018-02-02 13:14:47

标签: twitter-bootstrap gis openlayers openlayers-3

我创建了两个按钮(使用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>

我还关闭了打开图层的默认缩放按钮:

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

现在我的问题是,如何使我的两个按钮适合放大和缩小?

谢谢!

1 个答案:

答案 0 :(得分:1)

在html文件中创建html按钮后,通过引用html中的id为每个按钮添加一个控件。之后,听听地图上的点击事件。

&#13;
&#13;
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);
    }
});
&#13;
<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>
&#13;
&#13;
&#13;