Last active
March 4, 2022 04:32
-
-
Save malwoodsantoro/40c86cb0cca6e4c502df009d192adc49 to your computer and use it in GitHub Desktop.
Hide/show map using an HTML button
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"> | |
<link href="https://api.mapbox.com/mapbox-gl-js/v2.7.0/mapbox-gl.css" rel="stylesheet"> | |
<script src="https://api.mapbox.com/mapbox-gl-js/v2.7.0/mapbox-gl.js"></script> | |
<style> | |
body { | |
margin: 0; | |
padding: 0; | |
} | |
#button { | |
display: block; | |
position: relative; | |
width: 100px; | |
height: 30px; | |
border: none; | |
border-radius: 5px; | |
font-size: 20px; | |
text-align: center; | |
color: #fff; | |
background: #66CDAA; | |
} | |
#map { | |
position: relative; | |
top: 0; | |
bottom: 0; | |
width: 500px; | |
height: 500px; | |
} | |
#hide { | |
position: relative; | |
top: 0; | |
bottom: 0; | |
width: 500px; | |
height: 500px; | |
background-color: #66CDAA; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="button">Hide/show </div> | |
<div id="hide"> | |
<div id="map"></div> | |
</div> | |
<script> | |
mapboxgl.accessToken = 'pk.eyJ1IjoibWFsLXdvb2QiLCJhIjoiY2oyZ2t2em50MDAyMzJ3cnltMDFhb2NzdiJ9.X-D4Wvo5E5QxeP7K_I3O8w'; | |
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 | |
}); | |
document.getElementById('button').addEventListener('click', function () { | |
var x = document.getElementById("map"); | |
if (x.style.display === "none") { | |
x.style.display = "block"; | |
} else { | |
x.style.display = "none"; | |
} | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment