Last active
March 4, 2022 04:37
-
-
Save malwoodsantoro/3f2591c1b48241e0236c7d3d8194bcc8 to your computer and use it in GitHub Desktop.
Show center coordinates while panning
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8" /> | |
<title>Display a map on a webpage</title> | |
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" /> | |
<script src="https://api.mapbox.com/mapbox-gl-js/v2.7.0/mapbox-gl.js"></script> | |
<link href="https://api.mapbox.com/mapbox-gl-js/v2.7.0/mapbox-gl.css" rel="stylesheet" /> | |
<style> | |
body { margin: 0; padding: 0; } | |
#map { position: absolute; top: 0; bottom: 0; width: 100%; } | |
#center { | |
font: 20px; | |
font-weight: 600; | |
position: absolute; | |
top: 10px; | |
right: 20px; | |
z-index: 1; | |
border-radius: 3px; | |
max-width: 20%; | |
background-color: pink; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="map"></div> | |
<div id='center'>center</div> | |
<script> | |
mapboxgl.accessToken = 'pk.eyJ1Ijoic3ZjLW9rdGEtbWFwYm94LXN0YWZmLWFjY2VzcyIsImEiOiJja2VtbnQ1aGwxZXEyMnhwaWRidzF4cHliIn0.rZ8AmESrT0cz1V3FgymCvg'; | |
var map = new mapboxgl.Map({ | |
container: 'map', // container id | |
style: 'mapbox://styles/mapbox/streets-v11', // style URL | |
center: [-74.5, 40], // starting position [lng, lat] | |
zoom: 9 // starting zoom | |
}); | |
map.on('move', function (e) { | |
document.getElementById('center').innerHTML = map.getCenter().toString(); //zoom | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment