限制地图平移到某个区域
将地图平移限制在特定的区域内;
<!DOCTYPE html>
<html lang="en">
<head>
<title>限制地图平移到某个区域</title>
<meta property="og:description" content="将地图平移限制在特定的区域内" />
<meta charset='utf-8'>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel='stylesheet' href='https://unpkg.com/maplibre-gl@5.5.0/dist/maplibre-gl.css' />
<script src='https://unpkg.com/maplibre-gl@5.5.0/dist/maplibre-gl.js'></script>
<style>
body { margin: 0; padding: 0; }
html, body, #map { height: 100%; }
.info-box {
position: absolute;
top: 10px;
left: 10px;
padding: 15px;
background: rgba(255, 255, 255, 0.9);
border-radius: 4px;
font-family: 'Arial', sans-serif;
max-width: 300px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
z-index: 1;
}
.info-box h3 {
margin: 0 0 10px 0;
font-size: 16px;
border-bottom: 1px solid #ddd;
padding-bottom: 5px;
}
.current-bounds {
margin-top: 10px;
font-size: 12px;
color: #666;
font-family: monospace;
}
.bounds-label {
font-weight: bold;
margin-top: 10px;
}
.toggle-bounds {
display: flex;
margin-top: 15px;
align-items: center;
}
.toggle-label {
margin-left: 10px;
}
/* 开关样式 */
.switch {
position: relative;
display: inline-block;
width: 44px;
height: 22px;
}
.switch input {
opacity: 0;
width: 0;
height: 0;
}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
transition: .4s;
border-radius: 22px;
}
.slider:before {
position: absolute;
content: "";
height: 18px;
width: 18px;
left: 2px;
bottom: 2px;
background-color: white;
transition: .4s;
border-radius: 50%;
}
input:checked + .slider {
background-color: #4285F4;
}
input:focus + .slider {
box-shadow: 0 0 1px #4285F4;
}
input:checked + .slider:before {
transform: translateX(22px);
}
.btn-area {
margin-top: 15px;
display: flex;
gap: 10px;
}
button {
padding: 6px 12px;
background-color: #4285F4;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 13px;
}
button:hover {
background-color: #3367D6;
}
</style>
</head>
<body>
<div id="map"></div>
<div class="info-box">
<h3>限制地图平移区域</h3>
<p>此示例演示如何将地图平移限制在特定的地理边界内。</p>
<div class="toggle-bounds">
<label class="switch">
<input type="checkbox" id="toggle-bounds" checked>
<span class="slider"></span>
</label>
<div class="toggle-label">启用边界限制</div>
</div>
<div class="bounds-label">当前限制区域:</div>
<div class="current-bounds" id="current-bounds">美国大陆</div>
<div class="btn-area">
<button id="btn-us">美国大陆</button>
<button id="btn-eu">欧洲</button>
<button id="btn-asia">亚洲</button>
</div>
</div>
<script>
const map = new maplibregl.Map({
container: 'map',
style: 'https://api.maptiler.com/maps/streets/style.json?key=get_your_own_OpIi9ZULNHzrESv6T2vL',
center: [-98, 38.88], // 美国中部
zoom: 3
});
// 添加导航控件
map.addControl(new maplibregl.NavigationControl());
// 定义限制区域
const bounds = {
us: [[-125, 24], [-66, 49]], // 美国大陆
eu: [[-15, 35], [30, 65]], // 欧洲
asia: [[60, 5], [150, 60]] // 亚洲
};
// 当前选择的区域
let currentBounds = 'us';
let boundsEnabled = true;
// 更新显示的当前边界
function updateBoundsDisplay() {
document.getElementById('current-bounds').textContent =
boundsEnabled ?
(currentBounds === 'us' ? '美国大陆' :
currentBounds === 'eu' ? '欧洲' : '亚洲') :
'限制已禁用';
}
// 设置地图边界
function setBounds(region) {
currentBounds = region;
if (boundsEnabled) {
// 使用 fitBounds 可以平滑过渡到新的区域
map.fitBounds(bounds[region], {
padding: 50,
duration: 1000
});
// 设置最大边界
map.setMaxBounds(new maplibregl.LngLatBounds(
bounds[region][0],
bounds[region][1]
));
}
updateBoundsDisplay();
}
// 启用或禁用边界限制
function toggleBounds(enabled) {
boundsEnabled = enabled;
if (enabled) {
// 重新应用当前区域的边界
map.setMaxBounds(new maplibregl.LngLatBounds(
bounds[currentBounds][0],
bounds[currentBounds][1]
));
} else {
// 清除边界限制
map.setMaxBounds(null);
}
updateBoundsDisplay();
}
// 初始化地图时设置边界
map.on('load', () => {
setBounds('us');
// 向地图添加边界矩形
map.addSource('bounds-source', {
'type': 'geojson',
'data': {
'type': 'Feature',
'properties': {},
'geometry': {
'type': 'Polygon',
'coordinates': [[
bounds.us[0],
[bounds.us[0][0], bounds.us[1][1]],
bounds.us[1],
[bounds.us[1][0], bounds.us[0][1]],
bounds.us[0]
]]
}
}
});
map.addLayer({
'id': 'bounds-layer',
'type': 'line',
'source': 'bounds-source',
'layout': {},
'paint': {
'line-color': '#F44336',
'line-width': 2,
'line-dasharray': [2, 1]
}
});
});
// 绑定按钮事件
document.getElementById('btn-us').addEventListener('click', () => {
setBounds('us');
// 更新边界可视化
updateBoundsVisualization();
});
document.getElementById('btn-eu').addEventListener('click', () => {
setBounds('eu');
// 更新边界可视化
updateBoundsVisualization();
});
document.getElementById('btn-asia').addEventListener('click', () => {
setBounds('asia');
// 更新边界可视化
updateBoundsVisualization();
});
// 开关边界限制
document.getElementById('toggle-bounds').addEventListener('change', function() {
toggleBounds(this.checked);
});
// 更新边界可视化
function updateBoundsVisualization() {
if (!map.getSource('bounds-source')) return;
map.getSource('bounds-source').setData({
'type': 'Feature',
'properties': {},
'geometry': {
'type': 'Polygon',
'coordinates': [[
bounds[currentBounds][0],
[bounds[currentBounds][0][0], bounds[currentBounds][1][1]],
bounds[currentBounds][1],
[bounds[currentBounds][1][0], bounds[currentBounds][0][1]],
bounds[currentBounds][0]
]]
}
});
}
</script>
</body>
</html>