好的,下面是一个使用前端技术(HTML、CSS 和 JavaScript)实现可视化基站地图分布的项目示例。我们将使用 Leaflet 这个流行的开源 JavaScript 库来创建交互式地图。同时,我们会用 Chart.js 来创建统计表。
项目结构
base-station-map/
├── index.html
├── style.css
└── script.js
1. index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>基站地图分布</title>
<link rel="stylesheet" href="https://unpkg.com/leaflet/dist/leaflet.css" />
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="map" style="height: 70vh; width: 100%;"></div>
<div id="stats" class="stats-window">
<canvas id="statsChart"></canvas>
</div>
<script src="https://unpkg.com/leaflet/dist/leaflet.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script src="script.js"></script>
</body>
</html>
2. style.css
body {
margin: 0;
padding: 0;
font-family: Arial, sans-serif;
}
#map {
height: 70vh;
width: 100%;
}
.stats-window {
position: absolute;
top: 10px;
right: 10px;
background: white;
padding: 10px;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
width: 300px;
}
3. script.js
document.addEventListener('DOMContentLoaded', function () {
// 初始化地图
const map = L.map('map').setView([39.9042, 116.4074], 12); // 北京市中心
// 添加地图瓦片图层
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
// 模拟基站数据
const baseStations = [
{ lat: 39.9042, lng: 116.4074, status: 'normal' }, // 正常
{ lat: 39.9142, lng: 116.4174, status: 'fault' }, // 故障
{ lat: 39.9242, lng: 116.4274, status: 'disconnected' }, // 断站
{ lat: 39.8942, lng: 116.3974, status: 'retired' }, // 退服
{ lat: 39.9342, lng: 116.4374, status: 'normal' },
{ lat: 39.8842, lng: 116.3874, status: 'fault' },
{ lat: 39.9442, lng: 116.4474, status: 'disconnected'},
{ lat: 39.8742, lng: 116.3774, status: 'retired' },
];
// 状态颜色映射
const statusColors = {
normal: 'green',
fault: 'red',
disconnected: 'orange',
retired: 'gray'
};
// 统计各状态基站数量
const stats = {
normal: 0,
fault: 0,
disconnected: 0,
retired: 0
};
baseStations.forEach(station => {
const color = statusColors[station.status];
L.circleMarker([station.lat, station.lng], {
radius: 8,
fillColor: color,
color: "#000",
weight: 1,
opacity: 1,
fillOpacity: 0.8
}).addTo(map);
// 更新统计数据
stats[station.status]++;
});
// 创建统计图表
const ctx = document.getElementById('statsChart').getContext('2d');
new Chart(ctx, {
type: 'doughnut',
data: {
labels: ['正常', '故障', '断站', '退服'],
datasets: [{
label: '基站状态统计',
data: [stats.normal, stats.fault, stats.disconnected, stats.retired],
backgroundColor: [
statusColors.normal,
statusColors.fault,
statusColors.disconnected,
statusColors.retired
],
borderColor: [
'rgba(0, 128, 0, 1)',
'rgba(255, 0, 0, 1)',
'rgba(255, 165, 0, 1)',
'rgba(128, 128, 128, 1)'
],
borderWidth: 1
}]
},
options: {
responsive: true,
plugins: {
legend: {
position: 'top',
},
title: {
display: true,
text: '基站状态统计'
}
}
}
});
});
解释
-
index.html:
- 引入了 Leaflet 的 CSS 和 JS 文件以及 Chart.js 的文件。
- 创建了一个
div 用于显示地图,另一个 div 用于显示统计信息。
-
style.css:
- 设置了页面的基本样式,确保地图占据大部分空间,统计窗口位于页面的右上角,并带有阴影和圆角。
-
script.js:
- 初始化 Leaflet 地图,并设置视图中心为北京市。
- 添加 OpenStreetMap 瓦片图层。
- 创建模拟的基站数据,包括纬度、经度和状态。
- 使用不同颜色表示不同状态的基站,并在地图上显示。
- 统计每种状态的基站数量。
- 使用 Chart.js 创建一个环形图,显示不同状态基站的数量统计。
运行这个项目后,你会看到一个显示基站分布的交互式地图,右上角有一个小窗口,里面有一个环形图,展示了不同状态基站的数量统计。不同状态的基站在地图上以不同的颜色显示。