<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Map Click Listener</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
<script src="https://tiles.locationiq.com/v3/libs/maplibre-gl/1.15.2/maplibre-gl.js"></script>
<link href="https://tiles.locationiq.com/v3/libs/maplibre-gl/1.15.2/maplibre-gl.css" rel="stylesheet" />
<script src="https://tiles.locationiq.com/v3/js/liq-styles-ctrl-libre-gl.js?v=0.1.8"></script>
<link href="https://tiles.locationiq.com/v3/css/liq-styles-ctrl-libre-gl.css?v=0.1.8" rel="stylesheet" />
<style>
            body { margin:0px; padding:0px; }
            #map { position:absolute; top:0px; bottom:0px; width:100%; }
        </style>
</head>
<body>
<style>
            
            /* CSS is set by JS */
            .marker {
                display: block;
                border: none;
                cursor: pointer;
                padding: 0;
            }

            .coordinates {
                background: rgba(0, 0, 0, 0.7);
                color: #fff;
                position: absolute;
                bottom: 40px;
                left: 10px;
                padding: 5px 10px;
                margin: 0;
                font-size: 14px;
                line-height: 18px;
                border-radius: 3px;
                display: none;
            }

        </style>
<div id="map"></div>
<pre id="coordinates" class="coordinates"></pre>
<script>
            
            //Add your LocationIQ Maps Access Token here (not the API token!)
            locationiq.key = 'pk.aa7f5d0539c5675b7f3429402939d8fa';
            //Define the map and configure the map's theme
            var map = new maplibregl.Map({
                container: 'map',
                style: locationiq.getLayer("Streets"),
                zoom: 12,
                center: [-122.42, 37.779]
            });

            //Marker can be style either while adding the marker using JS or separately using CSS

            // First create DOM element for the marker
            var el = document.createElement('div');
            el.className = 'marker';
            // Set marker properties using JS
            el.style.backgroundImage = 'url(https://tiles.locationiq.com/static/images/marker50px.png)';
            el.style.width = '50px';
            el.style.height = '50px';

            map.on('click', function(e) {
                // e.point is the x, y coordinates of the mousemove event relative
                // to the top-left corner of the map
                // e.lngLat is the longitude, latitude geographical position of the event

                // Finally, create the marker where the mouse was clicked
                var marker = new maplibregl.Marker(el)
                    .setLngLat(e.lngLat.wrap())
                    .addTo(map);

                var lngLat = marker.getLngLat();
                coordinates.style.display = 'block';
                coordinates.innerHTML =
                    'Latitude: ' + lngLat.lat + '<br />Longitude: ' + lngLat.lng;
                
                });

        </script>
</body>
</html>