Marker
Runnable MapLibre GL JS example: Marker. Open the live demo, read the complete source, and copy the file into your project. No build step or API key.
single HTML file · CDN dependencies onlyinterface may contain Chinese labels
Source
<!--
示例:添加标记
核心逻辑:用 maplibregl.Marker 在城市坐标上放标记,可批量添加与清空
依赖:MapLibre GL JS 4 · jsDelivr CDN · demotiles 免 Key 样式
-->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta name="robots" content="noindex,follow">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>添加标记</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/maplibre-gl@4/dist/maplibre-gl.css">
<style>
html,body{height:100%;margin:0}
body{display:flex;flex-direction:column;overflow:hidden;
font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif;color:#16181d}
.bar{flex:0 0 auto;display:flex;gap:10px;align-items:center;padding:8px 12px;
background:#f6f7f9;border-bottom:1px solid #e3e6ea;font-size:12px;color:#58606e;flex-wrap:wrap}
.bar button{font:inherit;font-size:12px;padding:4px 10px;border-radius:4px;cursor:pointer;
border:1px solid #ccd1d9;background:#fff;color:#16181d}
.bar button:hover{background:#eef0f3}
.bar b{color:#16181d;font-weight:600}
#map{flex:1 1 auto;min-height:0}
</style>
</head>
<body>
<div class="bar">
<button id="add">放置标记</button>
<button id="clear">清空</button>
<span>共 <b id="n">0</b> 个标记</span>
<span id="msg">点击标记查看名称</span>
</div>
<div id="map"></div>
<script src="https://cdn.jsdelivr.net/npm/maplibre-gl@4/dist/maplibre-gl.js"></script>
<script>
(function () {
'use strict';
if (typeof maplibregl === 'undefined') {
document.body.innerHTML =
'<div style="padding:24px;color:#b42318">MapLibre 加载失败,请检查能否访问 cdn.jsdelivr.net</div>';
return;
}
var map = new maplibregl.Map({
container: 'map',
style: 'https://demotiles.maplibre.org/style.json',
center: [108.5, 34.5],
zoom: 3,
attributionControl: {
compact: true,
customAttribution: 'MapLibre demo tiles'
}
});
var cities = [
{ name: '北京', c: [116.391, 39.907], color: '#1f5fd0' },
{ name: '上海', c: [121.474, 31.230], color: '#c2410c' },
{ name: '广州', c: [113.264, 23.129], color: '#17694a' },
{ name: '成都', c: [104.066, 30.572], color: '#7c3aed' },
{ name: '乌鲁木齐', c: [87.617, 43.792], color: '#a16207' },
{ name: '哈尔滨', c: [126.642, 45.756], color: '#0e7490' }
];
var markers = [];
function addMarkers() {
clearMarkers();
cities.forEach(function (city) {
var m = new maplibregl.Marker({ color: city.color })
.setLngLat(city.c)
.addTo(map);
m.getElement().addEventListener('click', function () {
document.getElementById('msg').textContent =
city.name + ' · ' + city.c[0].toFixed(3) + ', ' + city.c[1].toFixed(3);
});
markers.push(m);
});
document.getElementById('n').textContent = markers.length;
}
function clearMarkers() {
markers.forEach(function (m) { m.remove(); });
markers = [];
document.getElementById('n').textContent = '0';
document.getElementById('msg').textContent = '点击标记查看名称';
}
document.getElementById('add').addEventListener('click', addMarkers);
document.getElementById('clear').addEventListener('click', clearMarkers);
map.addControl(new maplibregl.NavigationControl(), 'top-right');
addMarkers();
})();
</script>
</body>
</html>
Language: the code and this page are in English; the full tutorial and API notes for this example are in Chinese. The demo itself is the authoritative version — it runs exactly as shown.