添加实时数据
添加实时GeoJSON数据到地图;
<!DOCTYPE html>
<html lang="en">
<head>
<title>添加实时数据</title>
<meta property="og:description" content="添加实时GeoJSON数据到地图" />
<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%; }
.map-overlay {
position: absolute;
bottom: 0;
right: 0;
background: rgba(255, 255, 255, 0.8);
margin-right: 20px;
font-family: 'Open Sans', sans-serif;
overflow: auto;
border-radius: 3px;
padding: 10px;
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
line-height: 24px;
max-width: 300px;
color: #222;
}
.legend {
background-color: #fff;
border-radius: 3px;
bottom: 30px;
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
padding: 10px;
}
.legend h4 {
margin: 0 0 10px;
}
.legend div span {
border-radius: 50%;
display: inline-block;
height: 10px;
margin-right: 5px;
width: 10px;
}
.updateTime {
font-size: 12px;
font-style: italic;
margin-top: 10px;
}
</style>
</head>
<body>
<div id="map"></div>
<div class="map-overlay legend">
<h4>美国地震实时数据</h4>
<div><span style="background-color: #f00"></span>震级 6+</div>
<div><span style="background-color: #fd5"></span>震级 5+</div>
<div><span style="background-color: #ff0"></span>震级 4+</div>
<div><span style="background-color: #0f0"></span>震级 3+</div>
<div><span style="background-color: #0ff"></span>震级 2+</div>
<div><span style="background-color: #06f"></span>震级 1+</div>
<div><span style="background-color: #f0f"></span>震级 0+</div>
<div id="updateTime" class="updateTime"></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: [-100, 40],
zoom: 3
});
// 请求实时地震数据的函数
function getEarthquakeData() {
// 使用USGS的地震数据API
return fetch('https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_day.geojson')
.then(response => response.json())
.catch(error => {
console.error('获取地震数据失败:', error);
return null;
});
}
// 更新地图上的地震数据
function updateEarthquakes() {
getEarthquakeData().then(data => {
if (!data) return;
// 更新最后更新时间
const updateTimeElement = document.getElementById('updateTime');
const now = new Date();
updateTimeElement.textContent = `上次更新: ${now.toLocaleTimeString()}`;
// 如果数据源已存在,直接更新数据
if (map.getSource('earthquakes')) {
map.getSource('earthquakes').setData(data);
} else {
// 首次加载时,创建数据源和图层
map.addSource('earthquakes', {
'type': 'geojson',
'data': data
});
map.addLayer({
'id': 'earthquakes-circle',
'type': 'circle',
'source': 'earthquakes',
'paint': {
'circle-radius': [
'interpolate',
['linear'],
['get', 'mag'],
0, 4,
6, 16
],
'circle-color': [
'interpolate',
['linear'],
['get', 'mag'],
0, '#f0f',
1, '#06f',
2, '#0ff',
3, '#0f0',
4, '#ff0',
5, '#fd5',
6, '#f00'
],
'circle-opacity': 0.75,
'circle-stroke-width': 1,
'circle-stroke-color': '#fff'
}
});
// 添加鼠标悬停交互
map.on('mouseenter', 'earthquakes-circle', () => {
map.getCanvas().style.cursor = 'pointer';
});
map.on('mouseleave', 'earthquakes-circle', () => {
map.getCanvas().style.cursor = '';
});
// 添加点击事件显示弹出窗口
map.on('click', 'earthquakes-circle', (e) => {
const coordinates = e.features[0].geometry.coordinates.slice();
const magnitude = e.features[0].properties.mag;
const place = e.features[0].properties.place;
const time = new Date(e.features[0].properties.time).toLocaleString();
new maplibregl.Popup()
.setLngLat(coordinates)
.setHTML(`<h3>震级 ${magnitude}</h3><p>${place}</p><p>时间: ${time}</p>`)
.addTo(map);
});
}
});
}
map.on('load', () => {
// 初始加载地震数据
updateEarthquakes();
// 每隔5分钟更新一次数据
setInterval(updateEarthquakes, 5 * 60 * 1000);
});
</script>
</body>
</html>