<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Draggable Marker</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: 0;
padding: 0;
}
#map {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
}
</style>
</head>
<body>
<style>
.marker {
display: block;
border: none;
cursor: pointer;
padding: 0;
width: 50px;
height: 50px;
}
.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]
});
var coordinates = document.getElementById('coordinates');
// First create DOM element for the marker
var el = document.createElement('div');
el.className = 'marker';
el.id = 'marker';
// Set marker properties using JS
el.style.backgroundImage = 'url(https://tiles.locationiq.com/static/images/marker50px.png)';
var marker = new maplibregl.Marker(el, {
draggable: true
}).setLngLat([-122.444733, 37.767443])
.addTo(map);
// After the mouse is released the following function is executed which updates the displayed lat and long
function onDragEnd() {
var lngLat = marker.getLngLat();
coordinates.style.display = 'block';
coordinates.innerHTML =
'Latitude: ' + lngLat.lat + '<br />Longitude: ' + lngLat.lng;
}
marker.on('dragend', onDragEnd);
</script>
</body>
</html>