Implementation of multiple popu display based on openlayer4

Posted by justinma on Sun, 05 Apr 2020 02:43:43 +0200

                     . You can click the official website to pop up the demo. Based on this logic, you can write a dynamic overlay layer and the dom nodes you need.
                            .

Source code

<!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
        })
      });
          //Pop up layer
      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>

Topics: Android