21
loading...
This website collects cookies to deliver better user experience
.html
extension. For this article, we will use index.html
. <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Adding a map to your website or web application using Mapbox</title>
<!-- Styles -->
</head>
<body>
<!-- Header -->
<header>
<h1 style="text-align: center;">Map with Mapbox</h1>
</header>
<!-- Main -->
<main>
<!-- Add your div tag -->
</main>
<!-- Scripts -->
</body>
</html>
link
below into the head
:
<link href='https://api.mapbox.com/mapbox-gl-js/v2.3.1/mapbox-gl.css' rel='stylesheet' />
script
tag below towards the bottom of the body
:
<script src='https://api.mapbox.com/mapbox-gl-js/v2.3.1/mapbox-gl.js'></script>
script
that accesses the Mapbox API and allocates a container:
<script>
mapboxgl.accessToken = 'Add your Access Token Here';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v11'
});
</script>
div
tag into the body
with an id
that matches the container in the script
above:
<div id='map' style='width: 400px; height: 300px;'></div>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Adding a map to yor website or web application using Mapbox</title>
<!-- Styles -->
<link href='https://api.mapbox.com/mapbox-gl-js/v2.3.1/mapbox-gl.css' rel='stylesheet' />
</head>
<body>
<!-- Header -->
<header>
<h1 style="text-align: center;">Map with Mapbox</h1>
</header>
<!-- Main -->
<main>
<div id='map' style='width: 400px; height: 300px; margin: 0 auto;'></div>
</main>
<!-- Scripts -->
<script src='https://api.mapbox.com/mapbox-gl-js/v2.3.1/mapbox-gl.js'></script>
<script>
mapboxgl.accessToken = 'Add your Access Token Here';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v11',
center: [28.112268, -26.270760], // [Longitude, Latitude]
zoom: 5 // Initial zoom
});
</script>
</body>
</html>