<!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 Lines</title>
    </head>
    <body>
        <style>
            .marker {
                display: block;
                border: none;
                border-radius: 50%;
                cursor: pointer;
                padding: 0;
            }
            
        </style>
        <div id="map"></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
                })
            });
 

            //Add markers from geojson. This list can be generated dynamically with an AJAX call as well.
            var geojson = {
                "type": "FeatureCollection",
                "features": [
                    {
                        "type": "Feature",
                        "properties": {
                            "message": "Foo",
                            "iconSize": [60, 60]
                        },
                        "geometry": {
                            "type": "Point",
                            "coordinates": [
                                -122.487377,
                                37.772487
                            ]
                        }
                    },
                    {
                        "type": "Feature",
                        "properties": {
                            "message": "Bar",
                            "iconSize": [50, 50]
                        },
                        "geometry": {
                            "type": "Point",
                            "coordinates": [
                                -122.461627,
                                37.752945
                            ]
                        }
                    },
                    {
                        "type": "Feature",
                        "properties": {
                            "message": "Baz",
                            "iconSize": [40, 40]
                        },
                        "geometry": {
                            "type": "Point",
                            "coordinates": [
                                -122.431415,
                                37.761766
                            ]
                        }
                    }
                ]
            };

            //Add markers to map
            geojson.features.forEach(function(marker) {
                // create a DOM element for the marker
                let markerEle = marker;
                var el = document.createElement('div');
                el.className = 'marker';
                el.style.backgroundImage = 'url(https://placekitten.com/g/' + marker.properties.iconSize.join('/') + '/)';
                el.style.width = marker.properties.iconSize[0] + 'px';
                el.style.height = marker.properties.iconSize[1] + 'px';

                //Instead of this click listener, we can attach a popup / infowindow to this marker (see `Add Popup to a marker` example)
                el.addEventListener('click', function() {
                    window.alert(markerEle.properties.message);
                });

                // add marker to map
                var marker = new ol.Overlay({
                        position: ol.proj.fromLonLat(marker.geometry.coordinates),
                        positioning: 'center-center',
                        element: el,
                        stopEvent: false,
                        className: 'marker'
                    });
                map.addOverlay(marker);
            });

            //Let's declare a line string
            var lineString1 = new ol.geom.LineString([
                        [-122.487377,37.772487],       //Start coordinates
                        [-122.461627,37.752945]       //End coordinates
                    ]);

            //Let's declare a line string
            var lineString2 = new ol.geom.LineString([
                        [-122.461627,37.752945],       //Start coordinates
                        [-122.431415,37.761766]       //End coordinates
                    ]);

            lineString1.transform('EPSG:4326', 'EPSG:3857');      //to transform the coordinate system 
            lineString2.transform('EPSG:4326', 'EPSG:3857');

            //Let's configure the style to the lineString
            var lineStyle = new ol.style.Style({
                stroke : new ol.style.Stroke({
                    color: '#B22222',
                    width: 5      //width of the line
                }
                )
            });

            //Let's create the feature for the line Strings
            var lineStringfeature1 = new ol.Feature(lineString1);
            var lineStringfeature2 = new ol.Feature(lineString2);

            //Let's add the style to the line string
            lineStringfeature1.setStyle(lineStyle);
            lineStringfeature2.setStyle(lineStyle);

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

            //Let’s create a vector layer, with a source created above
            var vectorLayer = new ol.layer.Vector({
                source: vectorSource,
                zIndex: 1
            });
            
            map.addLayer(vectorLayer);
            olms.apply(map, styleJson);
        </script>
    </body>
</html>