我来详细指导你使用Vue和Mars3D创建三维地球场景项目。
第一步:环境准备
首先确保你的开发环境已安装:
- Node.js (建议14.0+版本)
- npm 或 yarn 包管理器
- 代码编辑器(如VS Code)
第二步:创建Vue项目
bash
# 使用Vue CLI创建项目
npm install -g @vue/cli
vue create mars3d-earth-project
# 或使用Vite创建(推荐,更快)
npm create vue@latest mars3d-earth-project
选择配置时建议选择:
- Vue 3
- TypeScript(可选)
- Router
- ESLint
进入项目目录:
bash
cd mars3d-earth-project
第三步:安装Mars3D依赖
bash
# 安装Mars3D核心库
npm install mars3d
# 安装Cesium(Mars3D基于Cesium)
npm install cesium
# 如果使用Vite,还需要安装vite插件
npm install vite-plugin-cesium --save-dev
第四步:配置构建工具
如果使用Vite,修改 vite.config.js:
javascript
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import cesium from 'vite-plugin-cesium'
export default defineConfig({
plugins: [
vue(),
cesium()
],
define: {
CESIUM_BASE_URL: JSON.stringify('/cesium/')
}
})
如果使用Vue CLI,修改 vue.config.js:
javascript
const { defineConfig } = require('@vue/cli-service')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const webpack = require('webpack')
const path = require('path')
module.exports = defineConfig({
transpileDependencies: true,
configureWebpack: {
plugins: [
new CopyWebpackPlugin({
patterns: [
{
from: path.join(__dirname, 'node_modules/cesium/Build/Cesium/Workers'),
to: 'cesium/Workers'
},
{
from: path.join(__dirname, 'node_modules/cesium/Build/Cesium/ThirdParty'),
to: 'cesium/ThirdParty'
},
{
from: path.join(__dirname, 'node_modules/cesium/Build/Cesium/Assets'),
to: 'cesium/Assets'
},
{
from: path.join(__dirname, 'node_modules/cesium/Build/Cesium/Widgets'),
to: 'cesium/Widgets'
}
]
}),
new webpack.DefinePlugin({
CESIUM_BASE_URL: JSON.stringify('./cesium/')
})
]
}
})
第五步:创建地球组件
在 src/components 目录下创建 Earth3D.vue:
vue
<template>
<div id="mars3dContainer" class="mars3d-container"></div>
</template>
<script>
import * as mars3d from 'mars3d'
export default {
name: 'Earth3D',
data() {
return {
map: null
}
},
mounted() {
this.initMars3D()
},
beforeUnmount() {
if (this.map) {
this.map.destroy()
this.map = null
}
},
methods: {
initMars3D() {
// Mars3D地图配置
const mapOptions = {
scene: {
center: { lat: 31.686, lng: 117.077, alt: 15000000, heading: 0, pitch: -90 },
showSun: true,
showMoon: true,
showSkyBox: true,
showSkyAtmosphere: true,
fog: true,
fxaa: true,
globe: {
showGroundAtmosphere: true,
enableLighting: true
}
},
basemaps: [
{
name: '天地图影像',
type: 'tdt',
layer: 'img_d',
key: ['你的天地图key'], // 需要申请天地图API key
show: true
}
],
layers: []
}
// 创建三维地球
this.map = new mars3d.Map('mars3dContainer', mapOptions)
// 地图初始化完成后的回调
this.map.on(mars3d.EventType.load, () => {
console.log('Mars3D地图初始化完成')
this.addEarthEffects()
})
},
addEarthEffects() {
// 添加大气效果
this.map.scene.skyAtmosphere.show = true
this.map.scene.globe.showGroundAtmosphere = true
// 设置地球光照
this.map.scene.globe.enableLighting = true
// 添加星空背景
this.map.scene.skyBox.show = true
// 可以添加更多效果,如粒子系统、动画等
this.addRotationAnimation()
},
addRotationAnimation() {
// 添加地球自转动画(可选)
let startTime = Date.now()
const rotationSpeed = 0.1 // 旋转速度
const animate = () => {
const currentTime = Date.now()
const deltaTime = (currentTime - startTime) / 1000
// 计算旋转角度
const rotation = deltaTime * rotationSpeed
// 应用旋转
this.map.camera.setView({
destination: this.map.camera.position,
orientation: {
heading: rotation,
pitch: this.map.camera.pitch,
roll: this.map.camera.roll
}
})
requestAnimationFrame(animate)
}
// 取消注释下面这行来启用自转动画
// animate()
}
}
}
</script>
<style scoped>
.mars3d-container {
width: 100%;
height: 100vh;
margin: 0;
padding: 0;
overflow: hidden;
}
</style>
第六步:修改主页面
修改 src/App.vue:
vue
<template>
<div id="app">
<Earth3D />
</div>
</template>
<script>
import Earth3D from './components/Earth3D.vue'
export default {
name: 'App',
components: {
Earth3D
}
}
</script>
<style>
#app {
margin: 0;
padding: 0;
width: 100%;
height: 100vh;
}
body, html {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
overflow: hidden;
}
</style>
第七步:申请地图服务密钥
为了显示地图底图,你需要申请相应的API密钥:
- 天地图密钥:访问 http://lbs.tianditu.gov.cn/ 申请
- 高德地图密钥:访问 https://console.amap.com/ 申请
- 百度地图密钥:访问 https://lbsyun.baidu.com/ 申请
将获得的密钥替换到配置中的 key 字段。
第八步:启动项目
bash
npm run serve
# 或者如果使用Vite
npm run dev
第九步:优化和扩展
你可以进一步添加功能:
- 添加控制面板:
vue
<!-- 在Earth3D.vue中添加 -->
<div class="control-panel">
<button @click="toggleRotation">切换自转</button>
<button @click="resetView">重置视角</button>
<button @click="toggleAtmosphere">切换大气</button>
</div>
- 添加图层管理:
javascript
// 添加不同的图层
addImageryLayer() {
const layer = new mars3d.layer.TdtLayer({
layer: 'vec_d',
key: '你的密钥'
})
this.map.addLayer(layer)
}
- 添加交互功能:
javascript
// 添加鼠标交互
this.map.on(mars3d.EventType.click, (event) => {
console.log('点击位置:', event.cartographic)
})
现在你应该能看到一个完整的三维地球场景了。如果遇到任何问题,检查浏览器控制台的错误信息,通常是密钥配置或依赖安装的问题。