哈希路由
使用哈希路由在URL中存储地图位置;
<!DOCTYPE html>
<html lang="en">
<head>
<title>哈希路由</title>
<meta property="og:description" content="使用哈希路由在URL中存储地图位置" />
<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%; }
</style>
</head>
<body>
<div id="map"></div>
<script>
const map = new maplibregl.Map({
container: 'map', // 容器ID
style: 'https://api.maptiler.com/maps/streets/style.json?key=get_your_own_OpIi9ZULNHzrESv6T2vL', // 样式URL
center: [-74.5, 40], // 起始位置 [经度, 纬度]
zoom: 9, // 起始缩放级别
hash: true // 哈希路由启用(如果URL中有哈希,将覆盖center和zoom)
// 注意:使用哈希功能后,当地图视图变化时,浏览器的URL会自动更新
// 当你与他人分享URL时,他们将看到相同的地图视图
// 格式为:#zoom/latitude/longitude
});
</script>
</body>
</html>