Introduction to Mapbox GL JS
Mapbox GL JS is a powerful open-source library for creating modern web maps. It enables developers to render interactive maps in web browsers with extensive customization options and rich mapping capabilities.
Setting Up Your Project
First, we need to include the Mapbox GL JavaScript library and its CSS in our HTML document. This can be done by adding the CDN links to the head section of your HTML file.
<!-- Include Mapbox GL JS library -->
<script src="https://api.mapbox.com/mapbox-gl-js/v2.12.0/mapbox-gl.js"></script>
<link
href="https://api.mapbox.com/mapbox-gl-js/v2.12.0/mapbox-gl.css"
rel="stylesheet"
/>
Authentication with Mapbox Access Token
Before creating a map, you need to authenticate with a Mapbox access token. This token should be obtained from your Mapbox account and included in your JavaScript code.
// Set your Mapbox access token
const mapToken = "pk.eyJ1IjoiY2hlbmdjaGFvODg2NiIsImEiOiJjbGhzcWowMHUwYTNyM2VwNXZhaXhjd3Q4In0.FEh2q7sEW88Z1B5GcK_TDg";
Creating the Map Instance
Once you have your acccess token, you can initialize a map instance. The initialization should be done after the DOM is fully loaded to ensure the map container exists.
// Initialize the map when the page loads
document.addEventListener('DOMContentLoaded', function() {
// Configure map options
const mapOptions = {
container: 'mapContainer', // ID of the map container element
style: 'mapbox://styles/mapbox/streets-v12', // Map style
center: [114.085947, 22.547], // Initial coordinates [longitude, latitude]
zoom: 12, // Initial zoom level
projection: 'globe' // Earth-like projection
};
// Create a new map instance
const interactiveMap = new mapboxgl.Map(mapOptions);
});
Map Container Styling
The map container must have defined dimensions to be visible on the page. Here's how you can style it using CSS:
<!-- HTML element to hold the map -->
<div id="mapContainer"></div>
/* CSS for the map container */
* {
margin: 0;
padding: 0;
}
#mapContainer {
width: 100vw;
height: 100vh;
}
Complete Implementation Example
Here's a complete HTML file that demonstrates how to create an interactive web map using Mapbox GL JS:
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Interactive Map with Mapbox GL JS</title>
<!-- Include Mapbox GL JS library -->
<script src="https://api.mapbox.com/mapbox-gl-js/v2.12.0/mapbox-gl.js"></script>
<link
href="https://api.mapbox.com/mapbox-gl-js/v2.12.0/mapbox-gl.css"
rel="stylesheet"
/>
<style>
* {
margin: 0;
padding: 0;
}
#mapContainer {
width: 100vw;
height: 100vh;
}
</style>
</head>
<body>
<div id="mapContainer"></div>
<script>
// Set your Mapbox access token
mapboxgl.accessToken = "pk.eyJ1IjoiY2hlbmdjaGFvODg2NiIsImEiOiJjbGhzcWowMHUwYTNyM2VwNXZhaXhjd3Q4In0.FEh2q7sEW88Z1B5GcK_TDg";
// Initialize the map when the page loads
document.addEventListener('DOMContentLoaded', function() {
// Configure map options
const mapConfiguration = {
container: 'mapContainer', // ID of the map container element
style: 'mapbox://styles/mapbox/streets-v12', // Map style
center: [114.085947, 22.547], // Initial coordinates [longitude, latitude]
zoom: 12, // Initial zoom level
projection: 'globe' // Earth-like projection
};
// Create a new map instance
const webMap = new mapboxgl.Map(mapConfiguration);
});
</script>
</body>
</html>
This implementation creates a full-screen interactive map centered on the specified coordinates with a street view style. The map supports zooming, panning, and other interactive features provided by Mapbox GL JS.