<!doctype html>
<html lang="en">
<head>
<link rel="stylesheet" href="https://tiles.locationiq.com/v3/libs/openlayers/8.2.0/ol.css" type="text/css">
<script src="https://tiles.locationiq.com/v3/libs/openlayers/8.2.0/ol.js"></script>
<style>
            body { margin:0px; padding:0px; }
            #map { position:absolute; top:0px; bottom:0px; width:100%; }
        </style>
<script src="https://tiles.locationiq.com/v3/libs/olms/12.0.0/olms.js" type="text/javascript"></script>
<title>Add Popup to a marker</title>
</head>
<body>
<style>
            .marker {
                display: block;
                border: none;
                cursor: pointer;
                padding: 0;
            }

            .ol-popup {
                position: absolute;
                background-color: white;
                box-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: 150px;
            }
            .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>
<div id="map"></div>
<div id="popup" class="ol-popup">
<a href="#" id="popup-closer" class="ol-popup-closer"></a>
<div id="popup-content"></div>
</div>
<script type="text/javascript">
            var key = 'pk.aa7f5d0539c5675b7f3429402939d8fa';   //Insert your LocationIQ access token here

            var styleJson = 'https://tiles-staging.locationiq.com/v3/streets/vector.json?key=' + key;

            const map = new ol.Map({
                target: 'map',
                view: new ol.View({
                    center: ol.proj.fromLonLat([-122.42, 37.779]),
                    zoom: 12
                })
            });
  

            //to create a marker
            var marker1 = new ol.Feature({
                        geometry: new ol.geom.Point(
                        ol.proj.fromLonLat([-122.444733, 37.767443])
                    ),
                    name: 'marker1'
                });
            
            //to enhance style and add icon to the map
            marker1.setStyle(new ol.style.Style({
                image: new ol.style.Icon({ 
                    scale: 0.5,               //scale to adjust the proportion of the icon   
                    src: 'https://tiles.locationiq.com/static/images/marker.png',           //link of the icon
                })
            }));

            //to create a marker
            var marker2 = new ol.Feature({
                    geometry: new ol.geom.Point(
                    ol.proj.fromLonLat([-122.4727000, 37.786258])
                ),
                name: 'marker2'  
            });
            
            //to enhance style and add icon to the map
            marker2.setStyle(new ol.style.Style({
                image: new ol.style.Icon({ 
                    scale: 0.5,                 //scale to adjust the proportion of the icon   
                    src: 'https://tiles.locationiq.com/static/images/marker50px.png',             //link to the icon
                })
            }));

            //Let’s include the markers and create a vector source with it
            var vectorSource = new ol.source.Vector({
                features: [marker1, marker2]          
            });

            //Let’s create a vector layer, with a source created above
            var vectorLayer = new ol.layer.Vector({
                source: vectorSource,
                zIndex: 1
            });
            
            //Elements that make up the popup.
            var container = document.getElementById('popup');
            var content = document.getElementById('popup-content');
            var closer = document.getElementById('popup-closer');

            //Create an overlay to anchor the popup to the map.
            var popup = new ol.Overlay({
                element: container,
                autoPan: true,
                autoPanAnimation: {
                    duration: 250
                }
            });

            //Add a click handler to hide the popup.
            closer.onclick = function() {
                popup.setPosition(undefined);
                closer.blur();
                return false;
            };

            map.addOverlay(popup);

            //Add on-click listener to the markers
            map.addEventListener('click', function(evt) {

                var feature = map.getFeaturesAtPixel(evt.pixel);
                if (feature !== null && feature.length > 0) {
                    var markerName = feature[0].get("name");
                    
                    if(markerName == 'marker1') {
                        let coordinate = feature[0].get("geometry")['flatCoordinates'];
                        content.innerHTML = '<b>Plate Number:</b> SF001<br> <b>Type:</b> Truck';  
                        popup.setPosition(coordinate);
                    }

                    if(markerName == 'marker2') {
                        let coordinate = feature[0].get("geometry")['flatCoordinates'];
                        content.innerHTML = '<b>Plate Number:</b> SF002<br> <b>Type:</b> Car';  
                        popup.setPosition(coordinate);
                    }
                }
            
            });

            map.addLayer(vectorLayer);

            olms.apply(map, styleJson);
        </script>
</body>
</html>