<!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://api.mapbox.com/mapbox-gl-js/v1.8.0/mapbox-gl.js'></script>
<link href='https://api.mapbox.com/mapbox-gl-js/v1.8.0/mapbox-gl.css' 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!)
            locationiqKey = 'pk.a5c3fbf2119bfb2275b62eddbccd76b3';
            //Define the map and configure the map's theme
            var map = new mapboxgl.Map({
                container: 'map',
                attributionControl: false, //need this to show a compact attribution icon (i) instead of the whole text
                style: 'https://tiles.locationiq.com/v3/streets/vector.json?key='+locationiqKey,
                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(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 mapboxgl.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>