### Vue 和 Mapbox 实现大量站点坐标绘制并基于客流量图例区分
为了在 Vue 中使用 Mapbox 绘制大量的站点坐标,并根据客流量通过图例进行区分,可以采用以下方式实现:
#### 1. **准备数据**
首先需要准备好站点的坐标数据以及对应的客流量信息。这些数据通常可以用 GeoJSON 格式存储。
```javascript
const stations = {
type: "FeatureCollection",
features: [
{ type: "Feature", geometry: { type: "Point", coordinates: [-74.5, 40] }, properties: { traffic: 100 } },
{ type: "Feature", geometry: { type: "Point", coordinates: [-74.6, 40.1] }, properties: { traffic: 200 } },
{ type: "Feature", geometry: { type: "Point", coordinates: [-74.7, 40] }, properties: { traffic: 300 } }
// 更多站点...
]
};
```
#### 2. **初始化地图**
在 Vue 的组件中初始化 Mapbox 地图,并加载基础的地图样式。
```html
<template>
<div id="map"></div>
</template>
<script setup>
import { onMounted } from 'vue';
import * as mapboxgl from 'mapbox-gl';
let map;
onMounted(() => {
mapboxgl.accessToken = 'your_mapbox_access_token'; // 替换为您的访问令牌
map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v11', // 基础地图样式
center: [-74.5, 40],
zoom: 9,
});
// 加载站点数据
loadStations();
});
function loadStations() {
const stations = {
type: "FeatureCollection",
features: [
{ type: "Feature", geometry: { type: "Point", coordinates: [-74.5, 40] }, properties: { traffic: 100 } },
{ type: "Feature", geometry: { type: "Point", coordinates: [-74.6, 40.1] }, properties: { traffic: 200 } },
{ type: "Feature", geometry: { type: "Point", coordinates: [-74.7, 40] }, properties: { traffic: 300 } }
// 更多站点...
]
};
// 添加站点数据作为源
map.addSource('stations-source', {
type: 'geojson',
data: stations
});
// 定义渲染规则
map.addLayer({
id: 'stations-layer',
type: 'circle',
source: 'stations-source',
paint: {
'circle-radius': ['interpolate', ['linear'], ['get', 'traffic'], 100, 5, 300, 20], // 动态调整半径大小[^1]
'circle-color': ['step', ['get', 'traffic'], '#FCAEAE', 150, '#FB6A4A', 250, '#CB181D'] // 根据客流量改变颜色[^2]
}
});
}
</script>
<style scoped>
#map {
width: 100%;
height: 100vh;
}
</style>
```
---
#### 3. **解释代码中的关键部分**
- **动态调整圆圈半径 (`circle-radius`):**
使用 `['interpolate', ['linear'], ...]` 表达式可以根据客流量数值动态调整圆圈的半径大小。例如,当客流量为 100 时,半径设为 5;当客流量达到 300 时,半径增大至 20[^1]。
- **根据客流量变化颜色 (`circle-color`):**
使用 `['step', ...]` 表达式定义分段的颜色映射关系。例如,客流量小于等于 150 显示浅红色 (#FCAEAE),大于 150 并小于等于 250 显示深红色 (#FB6A4A),超过 250 则显示更深的红 (#CB181D)[^2]。
---
#### 4. **添加图例支持**
为了让用户更直观地理解不同颜色和大小代表的意义,可以在页面底部添加一个简单的图例说明。
```html
<div class="legend">
<span><svg viewBox="0 0 10 10"><circle cx="5" cy="5" r="5" fill="#FCAEAE"/></svg> 低客流 (≤150)</span>
<span><svg viewBox="0 0 10 10"><circle cx="5" cy="5" r="5" fill="#FB6A4A"/></svg> 中等客流 (151~250)</span>
<span><svg viewBox="0 0 10 10"><circle cx="5" cy="5" r="5" fill="#CB181D"/></svg> 高客流 (>250)</span>
</div>
<style scoped>
.legend {
position: absolute;
bottom: 10px;
left: 10px;
background: white;
padding: 10px;
border-radius: 5px;
}
.legend span {
display: inline-block;
margin-right: 10px;
}
.legend svg circle {
stroke: black;
stroke-width: 1;
}
</style>
```
---
#### 5. **优化性能**
对于大规模的数据集(成千上万的站点),建议采取以下措施提升性能:
- **简化几何形状:** 如果可能,减少每个点的复杂度。
- **集群化:** 当缩放级别较低时,将附近的点聚合成单个图标或圆形区域[^3]。
- **懒加载:** 只有当视口内的数据才被加载到地图上。
---
###