OL Display overlay in any world

openlayers overlay in each world

by shrinath8487

HTML

<head>
  <link href="https://cdnjs.cloudflare.com/ajax/libs/openlayers/4.6.5/ol.css"></link>
</head>

<body>
  <div id="popup" title="ROUTECAST" class="ol-popup">
    <a href="#" id="popupcloser" class="ol-popup-closer"></a>
    <div id="popupcontent"></div>
  </div>
  <div id="map" class="map" style="height:600px;width:1024px;">
</body>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/openlayers/4.6.5/ol.js"></script>

CSS

.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: 180px;
}

.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: "";
}

JavaScript

var container = document.getElementById('popup');
var content = document.getElementById('popupcontent');
var closer = document.getElementById('popupcloser');

 var style = new ol.style.Style({
        image: new ol.style.Circle({
            radius: 5,
            fill: new ol.style.Fill({
                color: '#7AC7F4'
            }),
            stroke: new ol.style.Stroke({
                color: '#624553',
                width: 2
            })

        }),
        stroke: new ol.style.Stroke({
            color: '#EF5FA2',
            width: 2
        })
    });
 var feat = new ol.Feature({
  geometry: new ol.geom.Point(ol.proj.transform([74.23, 18.53], 'EPSG:4326',
    'EPSG:3857')),
  type: 'Point',
  desc: "Custom objects bound to this description"
 
});

var view = new ol.View({
        center: new ol.proj.transform([74, 18], 'EPSG:4326', 'EPSG:3857'),
        zoom: 2,
        minZoom: 1,
        projection: 'EPSG:3857'
    });
    
var drawingSource = new ol.source.Vector({
  wrapX: true,
  features:[feat]
});

var drawingLayer = new ol.layer.Vector({
  source: drawingSource,
  style: style
});
var baseLayer = new ol.layer.Tile({
  source: new ol.source.OSM()
});

//Map
var map = new ol.Map({
  layers: [baseLayer,drawingLayer],
  target: 'map',
  view: view,
  controls: [new ol.control.Zoom()]
});

var popup = new ol.Overlay({
  element: container,
  autoPanAnimation: {
    duration: 250
  }
});
map.addOverlay(popup);
popup.setPosition(undefined);

map.on('click', function(evt) {
  let feature = map.forEachFeatureAtPixel(evt.pixel, function(feat, layer) {
    return feat;
  });
  if (feature && feature.get('type') == 'Point') {
    let coordinate = evt.coordinate;
    content.innerHTML = feature.get('desc');
    popup.setPosition(coordinate);
  } else
    popup.setPosition(undefined);
});