1. Introduction and positioning
Mapbox GL JS is a high-performance JavaScript map library based on WebGL for building interactive 2D/3D vector map applications.Unlike traditional maps that use image tiles, Mapbox GL uses Vector Tiles + GPU rendering to achieve:
- Live style changes
- Smooth zoom
- 3D architecture
- terrain
- Dynamic data visualization
Therefore in Data visualization, city digital twin, logistics monitoring, large-screen visualization Very common in scenarios such as this.
Comparison of mainstream WebGIS libraries
| Library | Features | Applicable scenarios |
|---|---|---|
| Leaflet | Lightweight, simple, with many plug-ins | Simple map application |
| OpenLayers | The most complete GIS function, supporting OGC standards | Professional GIS system |
| MapLibre GL | Open source fork of Mapbox v1 | Free vector maps |
| Mapbox GL JS | Best visual effects, strong 3D | Business map visualization |
| Cesium | Focus on 3D Earth | Digital Earth, Aerospace |
Simple understanding:
- Leaflet → lightweight map
- OpenLayers → GIS Tools
- Mapbox → Visual Map
- Cesium → 3D Earth
Mapbox GL JS v3 capabilities used here
The CDN examples use version 3.19.0. These are relevant capabilities, not a claim that every feature first appeared in v3; check style and service prerequisites against the matching documentation.
1)Standard Style
A new default map style with a more realistic visual representation.
2) 3D lighting system
- Realistic shadows of buildings
- Sunshine simulation
- Light angle control
3)3D Terrain
Realistic terrain rendering.
4) New Camera API
Smoother camera controls.
5) Performance improvement
GPU rendering optimization, large-scale data is smoother.
What can you do after studying?
For example:
Real-time logistics monitoring
车辆位置 + 轨迹动画City Digital Twin
3D 建筑 + 实时数据Data visualization big screen
热力图 + 聚合点Immersive city tour
3D 地形 + fly 动画2. Development environment setup
Method 1: CDN (quickest way to get started)
Suitable for quick experience
<link
href="https://api.mapbox.com/mapbox-gl-js/v3.19.0/mapbox-gl.css"
rel="stylesheet"
/>
<script src="https://api.mapbox.com/mapbox-gl-js/v3.19.0/mapbox-gl.js"></script>Method 2: Vite + npm (recommended)
Initialization project:
npm create vite@latest mapbox-demo
cd mapbox-demo
npm installInstall Mapbox:
npm install mapbox-glmain.js:
import mapboxgl from "mapbox-gl";
import "mapbox-gl/dist/mapbox-gl.css";Start:
npm run devMethod 3: Pure ESM
<script type="module">
import mapboxgl from "https://cdn.skypack.dev/mapbox-gl";
</script>Get Mapbox Token
Need to register an account:
Get:
Access TokenMinimal runnable example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Mapbox Demo</title>
<link
href="https://api.mapbox.com/mapbox-gl-js/v3.19.0/mapbox-gl.css"
rel="stylesheet"
/>
<style>
body {
margin: 0;
}
#map {
width: 100vw;
height: 100vh;
}
</style>
</head>
<body>
<div id="map"></div>
<script src="https://api.mapbox.com/mapbox-gl-js/v3.19.0/mapbox-gl.js"></script>
<script>
mapboxgl.accessToken = "YOUR_TOKEN";
const map = new mapboxgl.Map({
container: "map",
style: "mapbox://styles/mapbox/standard",
center: [-118.24, 34.05],
zoom: 10,
});
</script>
</body>
</html>3. Quick overview of core concepts
It's important to understand the core structure of Mapbox.
Map
Map is a map instance
const map = new mapboxgl.Map({
container: "map",
style: "mapbox://styles/mapbox/standard",
});Camera
Control the map perspective.
map.flyTo({
center: [116.39, 39.9],
zoom: 12,
pitch: 60,
});Style
Map style system.
Follow Mapbox Style Spec
style: "mapbox://styles/mapbox/satellite-streets-v12";Sources
data source.
Common types:
geojson
vector
raster
raster-demExample:
map.addSource("points", {
type: "geojson",
data: "points.geojson",
});Layers
Layers are used to display data.
Common types:
fill
line
circle
symbol
fill-extrusionExample:
map.addLayer({
id: "point-layer",
type: "circle",
source: "points",
});Expressions
Mapbox’s data-driven styling system
'circle-color':[
'match',
['get','type'],
'restaurant','#ff0000',
'park','#00ff00',
'#0000ff'
]Controls
Map control:
NavigationControl
GeolocateControl
ScaleControlExample:
map.addControl(new mapboxgl.NavigationControl());Popup
Information pop-up window.
new mapboxgl.Popup().setLngLat([116, 39]).setHTML("<h3>北京</h3>").addTo(map);4. Make your first cool map in 5 minutes
Complete example:
<div id="map"></div>
<script>
mapboxgl.accessToken = "TOKEN";
const map = new mapboxgl.Map({
container: "map",
style: "mapbox://styles/mapbox/standard",
center: [-118.24, 34.05],
zoom: 10,
pitch: 60,
bearing: -20,
});
map.addControl(new mapboxgl.NavigationControl());
map.addControl(
new mapboxgl.GeolocateControl({
positionOptions: { enableHighAccuracy: true },
trackUserLocation: true,
}),
);
map.on("load", () => {
map.addSource("mapbox-dem", {
type: "raster-dem",
url: "mapbox://mapbox.mapbox-terrain-dem-v1",
});
map.setTerrain({
source: "mapbox-dem",
exaggeration: 1.5,
});
});
</script>Effect:
- 3D terrain
- control
- tilt angle
5. Intermediate Advanced
5.1 Switch map style
map.setStyle("mapbox://styles/mapbox/satellite-streets-v12");Common:
standard
streets
satellite
light
dark5.2 Add GeoJSON data
map.addSource("earthquakes", {
type: "geojson",
data: "earthquake.geojson",
});
map.addLayer({
id: "earthquake-layer",
type: "circle",
source: "earthquakes",
paint: {
"circle-radius": 6,
"circle-color": "red",
},
});5.3 Data-driven style
'circle-radius':[
'interpolate',
['linear'],
['zoom'],
5,2,
15,20
]5.4 3D Architecture
map.addLayer({
id: "3d-buildings",
source: "composite",
"source-layer": "building",
type: "fill-extrusion",
paint: {
"fill-extrusion-height": ["get", "height"],
"fill-extrusion-color": "#aaa",
},
});5.5 Interaction
Click event:
map.on("click", "poi-layer", (e) => {
new mapboxgl.Popup().setLngLat(e.lngLat).setHTML("POI").addTo(map);
});5.6 flyTo animation
map.flyTo({
center: [121.47, 31.23],
zoom: 12,
pitch: 60,
speed: 0.8,
});5.7 Aggregation
map.addSource("points", {
type: "geojson",
data: "points.geojson",
cluster: true,
clusterRadius: 50,
});6. Advanced practice: urban real-time traffic monitoring large screen
Project features:
实时车辆
交通热力图
轨迹动画
3D 建筑
POI 筛选
时间轴回放Project structure
mapbox-traffic
├ index.html
├ main.js
├ style.css
└ data
└ vehicles.geojsonLoad vehicle data
map.addSource("vehicles", {
type: "geojson",
data: "data/vehicles.geojson",
});Vehicle layer
map.addLayer({
id: "vehicle-layer",
type: "circle",
source: "vehicles",
paint: {
"circle-radius": 5,
"circle-color": "#ff0000",
},
});Aggregate a large number of vehicles
cluster: true;heat map
map.addLayer({
id: "traffic-heat",
type: "heatmap",
source: "vehicles",
paint: {
"heatmap-radius": 20,
},
});vehicle trajectory
map.addLayer({
id: "routes",
type: "line",
source: "routes",
paint: {
"line-color": "#00ffff",
"line-width": 4,
},
});WebSocket real-time updates
socket.onmessage = (e) => {
const data = JSON.parse(e.data);
map.getSource("vehicles").setData(data);
};Vue UI linkage
watch(selectedVehicle, (id) => {
map.flyTo({
center: vehicleMap[id],
});
});7. Production-level best practices
Token security
Don't hardcode the token.
Suggestions:
.envPerformance optimization
Reduce the number of layers.
推荐 < 100Use:
promoteId
cluster
vector tilesMobile terminal adaptation
antialias: true;Adapted to touch.
FAQ
Map white screen
Usually:
token 错误
style 错误Wrong coordinates
Mapbox uses:
[longitude, latitude]8. Learning resources
Official documentation:
https://docs.mapbox.com/mapbox-gl-js/
Example library:
Mapbox ExamplesRecommended advanced directions:
Mapbox + React
Mapbox + Deck.gl
Mapbox + Three.js
Mapbox + PMTiles9. Conclusion
Suggested learning paths for Mapbox GL JS:
CDN demo
↓
Vite 项目
↓
GeoJSON 数据
↓
表达式样式
↓
3D 地形
↓
实时数据
↓
完整地图产品Suggestions:
- 1️⃣ Fork official examples
- 2️⃣ Transformation demo
- 3️⃣ Publish your own map project
When you become proficient in Mapbox, you will be able to develop:
- City Digital Twin
- Logistics Monitoring
- Data Visualization Map
- Immersive WebGIS
Map development is The intersection of front-end + GIS + visualization 。