如何在OpenLayers中将HTML元素放置在地图上

时间:2018-08-31 14:17:40

标签: dom openlayers openlayers-3 angular-openlayers openlayers-5

我正在使用jQuery 3.21开发Openlayers 5.13。我试图将html div元素放置在openlayers中地图的视口上。该元素应该具有两个复选框,以过滤出地图上的某些内容。 为此,我使用了Overlay实例。 有两个问题: 1)当我放大或缩小时,覆盖层倾向于增加和减小尺寸,这不是我所期望的。 我希望该叠加层(html div元素)保持其大小。

2)我无法退出,想出如何将叠加层放置在右上角。 使用position属性实例化叠加层,我不知道该设置为什么。

我也不知道是否应该使用叠加层在地图上显示一些静态元素。 (我非常怀疑覆盖是否正确)

这是我的代码: css-

<style>
        .ol-panel {
            position: absolute;
            background-color: white;
            filter: drop-shadow(0 1px 4px rgba(0,0,0,0.2));
            padding: 15px;
            border-radius: 10px;
            border: 1px solid #cccccc;
            bottom: 12px;
            left: -50px;
            min-width: 100px;
        }
    </style>

html-

   <div id="panel" class="ol-panel">
        <div id="content">
            <table>
                <tr>
                    <td>
                       Ports &nbsp;<input type="checkbox">
                    </td>
                </tr>
                <tr>
                    <td>
                        Vessels&nbsp;<input type="checkbox">
                    </td>
                </tr>
            </table>
        </div>
    </div>

    <div id="map"></div>

脚本-

map = new ol.Map({
    logo: 'false',
    target: 'map',
    layers: [new ol.layer.Tile({
        title: 'OSM',
        type: 'base',
        visible: true,
        source: new ol.source.OSM()
    })],
    view: new ol.View({
        center: ol.proj.transform([17.813988, 43.342019], 'EPSG:4326', 'EPSG:3857'),
        zoom: 3
    })
});

panelDiv = document.getElementById("panel");
var panel = new ol.Overlay({
    element: panelDiv,
    stopEvent: false,
    //offset:[0,0],
    autoPan: true,
    position: ol.proj.transform([82,80 ], 'EPSG:4326', 'EPSG:3857'),
    positioning: 'top-right',
    autoPanAnimation: {
        duration: 250
    }
});
map.addOverlay(panel);

这是我当前的输出: current output

这是我所期望的,一个固定在某个位置的元素:

Expected

参考-[http://openlayers.org/en/latest/apidoc/module-ol_Overlay-Overlay.html]

1 个答案:

答案 0 :(得分:0)

我在这里找到了想要的东西[http://openlayers.org/en/latest/examples/custom-controls.html?q=custom-controls]

/** Create a checkbox to toggle visibility of straits on map 
             *  You can create different controls for different operations
             */
            var strait_cbx = document.createElement('input');
            strait_cbx.type = "checkbox";
            strait_cbx.id = "strait_cbx";
            strait_cbx.checked = true;

 /**
             * This is a container element which holds input elements which are controls for the map
             * You can add as many as you want depending upon use.
             * The element is a div which would act like a panel to add controls on map like toggling visibility of the layers
             */
            var element = document.createElement('div');
            element.className = 'ol-control-panel ol-unselectable ol-control';
            element.innerHTML="<b>Straits</b>&nbsp;"
            element.appendChild(strait_cbx);

            /*A custom control which has container holding input elements etc*/
            var controlPanel = new ol.control.Control({
                element: element
            });
map.addControl(controlPanel);

output