vue百度地图绘制台风圈
时间: 2025-07-21 15:31:01 浏览: 3
在 Vue 框架中使用百度地图 API 绘制台风路径或影响范围,可以通过结合百度地图的 JavaScript API 和 Vue 的生命周期钩子来实现。以下是一个完整的实现思路和示例代码。
### 初始化地图容器
首先,在 Vue 组件中创建一个地图容器,并在 `mounted` 生命周期钩子中初始化百度地图实例:
```vue
<template>
<div id="container" style="width: 100%; height: 100vh;"></div>
</template>
<script>
export default {
name: "TyphoonMap",
data() {
return {
map: null,
typhoonPath: [
{ lng: 120.1, lat: 30.2 },
{ lng: 120.5, lat: 30.6 },
{ lng: 121.0, lat: 31.0 },
{ lng: 121.5, lat: 31.5 },
],
};
},
mounted() {
this.initMap();
},
methods: {
initMap() {
const map = new BMapGL.Map("container");
this.map = map;
// 设置中心点
const point = new BMapGL.Point(120.1, 30.2);
map.centerAndZoom(point, 7);
map.enableScrollWheelZoom(true);
// 绘制台风路径
this.drawTyphoonPath();
// 绘制台风影响范围(可选)
this.drawTyphoonCircle();
},
drawTyphoonPath() {
const path = this.typhoonPath.map(p => new BMapGL.Point(p.lng, p.lat));
const polyline = new BMapGL.Polyline(path, {
strokeColor: "red",
strokeWeight: 4,
strokeStyle: "dashed",
});
this.map.addOverlay(polyline);
},
drawTyphoonCircle() {
const center = new BMapGL.Point(121.0, 31.0);
const circle = new BMapGL.Circle(center, 100000, {
strokeColor: "blue",
strokeWeight: 2,
fillColor: "#1791fc",
fillOpacity: 0.4,
});
this.map.addOverlay(circle);
},
},
};
</script>
```
### 动态加载台风数据
如果需要从本地或远程加载台风路径数据,可以使用 `import` 引入 JSON 文件,如引用[2]所述:
```js
import typhoon_202413 from "../assets/typhoon/data/typhoon_202413_BEBINCA.json";
```
然后在 `mounted` 中设置路径:
```js
mounted() {
this.typhoonPath = typhoon_202413.path;
this.initMap();
}
```
### 台风路径样式与交互
- 路径线可设置不同样式,如虚线、实线、颜色、粗细等。
- 可添加点击事件,点击路径点显示详细信息。
- 可结合 `BMapGL.Marker` 添加台风路径上的标记点。
```js
addMarkers() {
this.typhoonPath.forEach((point) => {
const marker = new BMapGL.Marker(new BMapGL.Point(point.lng, point.lat));
this.map.addOverlay(marker);
marker.addEventListener("click", () => {
const infoWindow = new BMapGL.InfoWindow("台风路径点");
this.map.openInfoWindow(infoWindow, marker.getPosition());
});
});
}
```
### 配置百度地图 API
在 `index.html` 中引入百度地图 API:
```html
<script type="text/javascript" src="https://api.map.baidu.com/api?v=3.0&ak=你的AK"></script>
```
确保替换 `你的AK` 为百度地图开放平台申请的合法密钥。
### 风险提示与优化建议
- **性能优化**:如果台风路径点过多,建议使用聚合标记或分段绘制。
- **数据安全**:API 密钥应避免暴露在前端代码中,建议通过服务端代理请求。
- **兼容性**:百度地图 API 在移动端和 PC 端表现略有不同,需做适配测试。
阅读全文
相关推荐

















