Draw Circle
Runnable MapLibre GL JS example: Draw Circle. 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
<!--
示例:画圆
核心逻辑:点击取圆心,按球面公式算出半径对应的 64 个边界点,拼成多边形源
数据:圆环坐标全部在页面内计算,不依赖几何计算库
依赖:MapLibre GL JS 4 · jsDelivr CDN · demotiles 样式
-->
<!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:14px;align-items:center;padding:8px 12px;
background:#f6f7f9;border-bottom:1px solid #e3e6ea;font-size:12px;color:#58606e;flex-wrap:wrap}
.bar b{color:#16181d;font-weight:600}
.bar input[type=range]{width:140px}
#map{flex:1 1 auto;min-height:0}
</style>
</head>
<body>
<div class="bar">
<span>圆心 <b id="c">116.390, 39.900</b></span>
<label>半径 <input id="r" type="range" min="10" max="300" step="10" value="80"> <b id="rv">80 km</b></label>
<span>点击地图任意位置重新定圆心</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 R = 6371.0088;
var RAD = Math.PI / 180;
/* 从圆心沿某方位角走 dist 公里,返回落点经纬度 */
function destination(lng, lat, bearing, dist) {
var d = dist / R;
var b = bearing * RAD;
var p1 = lat * RAD;
var l1 = lng * RAD;
var p2 = Math.asin(
Math.sin(p1) * Math.cos(d) + Math.cos(p1) * Math.sin(d) * Math.cos(b)
);
var l2 = l1 + Math.atan2(
Math.sin(b) * Math.sin(d) * Math.cos(p1),
Math.cos(d) - Math.sin(p1) * Math.sin(p2)
);
return [((l2 / RAD + 540) % 360) - 180, p2 / RAD];
}
function circlePolygon(lng, lat, radiusKm) {
var pts = [];
for (var i = 0; i <= 64; i++) {
pts.push(destination(lng, lat, i / 64 * 360, radiusKm));
}
return pts;
}
var center = [116.39, 39.90];
var radius = 80;
var map = new maplibregl.Map({
container: 'map',
style: 'https://demotiles.maplibre.org/style.json',
center: center,
zoom: 6,
attributionControl: { compact: true, customAttribution: 'MapLibre demo tiles' }
});
map.addControl(new maplibregl.NavigationControl(), 'top-right');
map.addControl(new maplibregl.ScaleControl({ maxWidth: 110 }), 'bottom-left');
var cEl = document.getElementById('c');
var rvEl = document.getElementById('rv');
map.on('load', function () {
map.addSource('circle', {
type: 'geojson',
data: {
type: 'FeatureCollection',
features: [{
type: 'Feature',
properties: {},
geometry: { type: 'Polygon', coordinates: [circlePolygon(center[0], center[1], radius)] }
}]
}
});
map.addLayer({
id: 'circle-fill',
type: 'fill',
source: 'circle',
paint: { 'fill-color': '#2e86de', 'fill-opacity': 0.18 }
});
map.addLayer({
id: 'circle-line',
type: 'line',
source: 'circle',
paint: { 'line-color': '#2e86de', 'line-width': 2, 'line-dasharray': [3, 2] }
});
map.addSource('center', {
type: 'geojson',
data: {
type: 'FeatureCollection',
features: [{
type: 'Feature',
properties: {},
geometry: { type: 'Point', coordinates: center }
}]
}
});
map.addLayer({
id: 'center-dot',
type: 'circle',
source: 'center',
paint: {
'circle-radius': 5,
'circle-color': '#f03e3e',
'circle-stroke-width': 2,
'circle-stroke-color': '#ffffff'
}
});
});
function update() {
if (!map.getSource('circle')) return;
map.getSource('circle').setData({
type: 'FeatureCollection',
features: [{
type: 'Feature',
properties: {},
geometry: { type: 'Polygon', coordinates: [circlePolygon(center[0], center[1], radius)] }
}]
});
map.getSource('center').setData({
type: 'FeatureCollection',
features: [{
type: 'Feature',
properties: {},
geometry: { type: 'Point', coordinates: center }
}]
});
cEl.textContent = center[0].toFixed(3) + ', ' + center[1].toFixed(3);
}
map.on('click', function (e) {
center = [e.lngLat.lng, e.lngLat.lat];
update();
});
document.getElementById('r').addEventListener('input', function () {
radius = parseInt(this.value, 10);
rvEl.textContent = radius + ' km';
update();
});
map.on('error', function (e) {
if (e.error) console.warn('地图错误:' + e.error.message);
});
})();
</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.