基於openlayer4實現多個popu展示
阿新 • • 發佈:2018-12-16
這個超出螢幕能自動平移,沒有試:
https://blog.csdn.net/qq_27186245/article/details/82227282
https://blog.csdn.net/qq_27186245/article/details/82227234
基於openlayer4實現多個popu展示
最近一個專案需要基於openlayer4實現類似於popu方式展現漁場具體資訊。翻看官網有點選彈出demo,基於此邏輯編寫動態生成overlay圖層及其所需要的dom節點。
在編寫改demo時,遇到一個特別問題:地圖載入osm底圖,座標點使用的是隨機三個座標點,popu始終表現為一個,最終通過設定view的投影,效果正常。
原始碼
<!DOCTYPE html> <html> <head> <title>Popup</title> <link rel="stylesheet" href="https://openlayers.org/en/v4.6.5/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.5/build/ol.js"></script> <style> .ol-popup { position: absolute; background-color: white; -webkit-filter: drop-shadow(0 1px 4px rgba(0,0,0,0.2)); 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: 50px; } .ol-popup:after, .ol-popup:before { top: 100%; border: solid transparent; content: " "; height: 0; width: 0; position: absolute; pointer-events: none; } .ol-popup:after { border-top-color: white; border-width: 10px; left: 48px; margin-left: -10px; } .ol-popup:before { border-top-color: #cccccc; border-width: 11px; left: 48px; margin-left: -11px; } .ol-popup-closer { text-decoration: none; position: absolute; top: 2px; right: 8px; } .ol-popup-closer:after { content: "✖"; } </style> </head> <body> <div id="map" class="map"></div> <script> var map = new ol.Map({ layers: [ new ol.layer.Tile({ source: new ol.source.OSM() }) ], target: 'map', view: new ol.View({ projection:"EPSG:4326", center: [115.55145, 23.01361], zoom: 9 }) }); //popu彈出層 var points=[[115.55145, 23.01361],[115.55145, 24.01361],[116.55145, 23.01361]]; points.forEach(function(v,i){ var tmp=document.createElement('div'); tmp.id="popup_"+i; tmp.className='ol-popup'; tmp.innerHTML='<div>hi hi</div>'; var overlay_tmp= new ol.Overlay({ element: tmp, autoPan: true, autoPanAnimation: { duration: 250 }, type:"mulPopu", position:v }); map.addOverlay(overlay_tmp); }); map.on('singleclick', function(evt) { }); </script> </body> </html>