1、使用npm安装依赖库
//安装mars3d主库
npm install mars3d --save
//安装mars3d插件(按需安装)
npm install mars3d-space --save
//安装copy-webpack-plugin
插件在第3步中使用,根据webpack版本安装,不匹配的版本可能出错,版本需要5.0
“copy-webpack-plugin”: “^5.0.0”,
npm install copy-webpack-plugin --save-dev
2.在main.js全局导入 或者 在使用mars3d的文件中导入(这一步可以跳过,极简地球只需要导入mars3d主库即可,其他需要时使用)
//引入cesium基础库
import "mars3d-cesium/Build/Cesium/Widgets/widgets.css";
import * as Cesium from "mars3d-cesium";
//导入mars3d主库
import "mars3d/dist/mars3d.css";
import * as mars3d from "mars3d";
//导入mars3d插件(按需使用,需要先npm install)
import "mars3d-space";
3.在vue.config.js 添加配置
'use strict'
const path = require('path')
const defaultSettings = require('./src/settings.js')
const webpack = require('webpack')
//地图的配置
const CopyWebpackPlugin = require('copy-webpack-plugin')
function resolve(dir) {
return path.join(__dirname, dir)
}
const CompressionPlugin = require('compression-webpack-plugin')
const name = process.env.VUE_APP_TITLE || '芋道管理系统' // 网页标题
const port = process.env.port || process.env.npm_config_port || 80 // 端口
// vue.config.js 配置说明
//官方vue.config.js 参考文档 https://cli.vuejs.org/zh/config/#css-loaderoptions
// 这里只列一部分,具体配置参考文档
module.exports = {
// 部署生产环境和开发环境下的URL。
// 默认情况下,Vue CLI 会假设你的应用是被部署在一个域名的根路径上
// 例如 https://www.ruoyi.vip/。如果应用被部署在一个子路径上,你就需要用这个选项指定这个子路径。例如,如果你的应用被部署在 https://www.ruoyi.vip/admin/,则设置 baseUrl 为 /admin/。
publicPath: process.env.PUBLIC_PATH ? process.env.PUBLIC_PATH : '/',
// 在npm run build 或 yarn build 时 ,生成文件的目录名称(要和baseUrl的生产环境路径一致)(默认dist)
outputDir: 'dist',
// 用于放置生成的静态资源 (js、css、img、fonts) 的;(项目打包之后,静态资源会放在这个文件夹下)
assetsDir: 'static',
// 是否开启eslint保存检测,有效值:ture | false | 'error'
lintOnSave: process.env.NODE_ENV === 'development',
// 如果你不需要生产环境的 source map,可以将其设置为 false 以加速生产环境构建。
productionSourceMap: false,
// webpack-dev-server 相关配置
devServer: {
host: '0.0.0.0',
port: port,
open: true,
proxy: {
// detail: https://cli.vuejs.org/config/#devserver-proxy
['/proxy-api']: {
target: `http://localhost:48080`,
// target: `http://api-dashboard.yudao.iocoder.cn`,
changeOrigin: true,
pathRewrite: {
['^' + process.env.VUE_APP_BASE_API]: ''
}
}
},
disableHostCheck: true
},
css: {
loaderOptions: {
sass: {
sassOptions: { outputStyle: "expanded" }
}
}
},
configureWebpack: (config) => {
let cesiumSourcePath = 'node_modules/mars3d-cesium/Build/Cesium/' //cesium库目录
let cesiumRunPath = './mars3d-cesium/' //cesium运行时主目录
let plugins = [
//标识cesium资源所在的主目录,cesium内部资源加载、多线程等处理时需要用到
new webpack.DefinePlugin({
CESIUM_BASE_URL: JSON.stringify(path.join(config.output.publicPath,cesiumRunPath))
}),
//cesium相关资源目录需要拷贝到系统目录下面
new CopyWebpackPlugin([{ from: path.join(cesiumSourcePath, 'Workers'), to: path.join(config.output.path,cesiumRunPath, 'Workers') }]),
new CopyWebpackPlugin([{ from: path.join(cesiumSourcePath, 'Assets'), to: path.join(config.output.path,cesiumRunPath, 'Assets') }]),
new CopyWebpackPlugin([{ from: path.join(cesiumSourcePath, 'ThirdParty'), to: path.join(config.output.path,cesiumRunPath, 'ThirdParty') }]),
new CopyWebpackPlugin([{ from: path.join(cesiumSourcePath, 'Widgets'), to: path.join(config.output.path,cesiumRunPath, 'Widgets') }]),
new CompressionPlugin({
cache: false, // 不启用文件缓存
test: /\.(js|css|html)?$/i, // 压缩文件格式
filename: '[path].gz[query]', // 压缩后的文件名
algorithm: 'gzip', // 使用gzip压缩
minRatio: 0.8 // 压缩率小于1才会压缩
}),
]
return {
name: name,
resolve: {
alias: {
'@': resolve('src')
}
},
plugins: plugins
}
},
chainWebpack(config) {
config.plugins.delete('preload') // TODO: need test
config.plugins.delete('prefetch') // TODO: need test
// set svg-sprite-loader
config.module
.rule('svg')
.exclude.add(resolve('src/assets/icons'))
.end()
config.module
.rule('icons')
.test(/\.svg$/)
.include.add(resolve('src/assets/icons'))
.end()
.use('svg-sprite-loader')
.loader('svg-sprite-loader')
.options({
symbolId: 'icon-[name]'
})
.end()
config
.when(process.env.NODE_ENV !== 'development',
config => {
config
.plugin('ScriptExtHtmlWebpackPlugin')
.after('html')
.use('script-ext-html-webpack-plugin', [{
// `runtime` must same as runtimeChunk name. default is `runtime`
inline: /runtime\..*\.js$/
}])
.end()
config
.optimization.splitChunks({
chunks: 'all',
cacheGroups: {
libs: {
name: 'chunk-libs',
test: /[\\/]node_modules[\\/]/,
priority: 10,
chunks: 'initial' // only package third parties that are initially dependent
},
elementUI: {
name: 'chunk-elementUI', // split elementUI into a single package
priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
},
commons: {
name: 'chunk-commons',
test: resolve('src/components'), // can customize your rules
minChunks: 3, // minimum common number
priority: 5,
reuseExistingChunk: true
}
}
})
config.optimization.runtimeChunk('single'),
{
from: path.resolve(__dirname, './public/robots.txt'), //防爬虫文件
to: './', //到根目录下
}
}
)
}
}
-----如果出错替换以上代码-----
//webpack也是需要导入的,
const webpack = require('webpack')
//导入copy-webpack-plugin插件
const CopyWebpackPlugin = require('copy-webpack-plugin')
// 在module.exports中添加下列配置
configureWebpack: (config) => {
const cesiumSourcePath = 'node_modules/mars3d-cesium/Build/Cesium/' // cesium库安装目录
const cesiumRunPath = './mars3d-cesium/' // cesium运行时路径
const plugins = [
// 标识cesium资源所在的主目录,cesium内部资源加载、多线程等处理时需要用到
new webpack.DefinePlugin({
CESIUM_BASE_URL: JSON.stringify(path.join(config.output.publicPath, cesiumRunPath))
}),
// Cesium相关资源目录需要拷贝到系统目录下面(部分CopyWebpackPlugin版本的语法可能需要patterns,代码在最下面)
new CopyWebpackPlugin([
{ from: path.join(cesiumSourcePath, 'Workers'), to: path.join(config.output.path, cesiumRunPath, 'Workers') },
{ from: path.join(cesiumSourcePath, 'Assets'), to: path.join(config.output.path, cesiumRunPath, 'Assets') },
{ from: path.join(cesiumSourcePath, 'ThirdParty'), to: path.join(config.output.path, cesiumRunPath, 'ThirdParty') },
{ from: path.join(cesiumSourcePath, 'Widgets'), to: path.join(config.output.path, cesiumRunPath, 'Widgets') }
])
]
return {
module: { unknownContextCritical: false }, // 配置加载的模块类型,cesium时必须配置
plugins: plugins
}
}
//如果出错请替换上面的代码
// new CopyWebpackPlugin({
// patterns: [
// { from: path.join(cesiumSourcePath, 'Workers'), to: path.join(config.output.path, cesiumRunPath, 'Workers') },
// { from: path.join(cesiumSourcePath, 'Assets'), to: path.join(config.output.path, cesiumRunPath, 'Assets') },
// { from: path.join(cesiumSourcePath, 'ThirdParty'), to: path.join(config.output.path, cesiumRunPath, 'ThirdParty') },
// { from: path.join(cesiumSourcePath, 'Widgets'), to: path.join(config.output.path, cesiumRunPath, 'Widgets') }
// ]
// })
**4. 在vue2中创建一个极简地球 需要创建地球配置初始化文件 mars3dConfig 在 当前页面引入 **文章来源:https://www.toymoban.com/news/detail-781479.html
<div class="grid-content bg-purple-light">
<div id="mars3dContainer" class="mars3d-container">
</div>
</div>
/*地图初始化*/
initMap(){
map = new mars3d.Map("mars3dContainer",mars3dConfig.map3d );
graphicLayer = new mars3d.layer.GraphicLayer();
map.addLayer(graphicLayer);
}
创建conifg
文章来源地址https://www.toymoban.com/news/detail-781479.html
{
"map3d":{
"scene": {
"center": {
"lat": 40.096899,
"lng": 116.358594,
"alt": 300,
"heading": 0,
"pitch": -45 },
"scene3DOnly": false,
"removeDblClick": true,
"sceneMode": 3,
"showSun": true,
"showMoon": true,
"showSkyBox": true,
"showSkyAtmosphere": true,
"fog": true,
"fxaa": true,
"globe": {
"showGroundAtmosphere": true,
"depthTestAgainstTerrain": false,
"baseColor": "#546a53",
"enableLighting": false
},
"cameraController": {
"zoomFactor": 3.0,
"minimumZoomDistance": 1,
"maximumZoomDistance": 50000000,
"enableRotate": true,
"enableZoom": true
}
},
"control": {
"baseLayerPicker": true,
"homeButton": true,
"sceneModePicker": true,
"navigationHelpButton": true,
"fullscreenButton": true,
"contextmenu": { "hasDefault": true },
"mouseDownView": true,
"zoom": {
"insertIndex": 1
},
"locationBar": {
"fps": true,
"crs": "CGCS2000_GK_Zone_3",
"crsDecimal": 0,
"template": "<div>经度:{lng}</div> <div>纬度:{lat}</div> <div class='hide1000'>横{crsx} 纵{crsy}</div> <div>海拔:{alt}米</div> <div class='hide700'>层级:{level}</div><div>方向:{heading}°</div> <div>俯仰角:{pitch}°</div><div class='hide700'>视高:{cameraHeight}米</div>"
}
},
"templateValues": {
"dataServer": "//data.mars3d.cn",
"gltfServerUrl": "//data.mars3d.cn/gltf"
},
"terrain": {
"url": "//data.mars3d.cn/terrain",
"show": true
},
"basemaps": [
{
"id": 10,
"name": "地图底图",
"type": "group"
},
{
"id": 2021,
"pid": 10,
"name": "天地图影像",
"icon": "/img/basemaps/tdt_img.png",
"type": "group",
"layers": [
{
"name": "底图",
"type": "tdt",
"layer": "img_d"
},
{
"name": "注记",
"type": "tdt",
"layer": "img_z"
}
],
"show": true
},
{
"pid": 10,
"name": "天地图电子",
"icon": "/img/basemaps/tdt_vec.png",
"type": "group",
"layers": [
{
"name": "底图",
"type": "tdt",
"layer": "vec_d"
},
{
"name": "注记",
"type": "tdt",
"layer": "vec_z"
}
]
},
{
"pid": 10,
"name": "高德影像",
"type": "group",
"icon": "/img/basemaps/gaode_img.png",
"layers": [
{
"name": "底图",
"type": "gaode",
"layer": "img_d"
},
{
"name": "注记",
"type": "gaode",
"layer": "img_z"
}
]
},
{
"pid": 10,
"name": "高德电子",
"type": "gaode",
"icon": "/img/basemaps/gaode_vec.png",
"layer": "vec"
},
{
"pid": 10,
"name": "百度影像",
"type": "group",
"icon": "/img/basemaps/bd-img.png",
"layers": [
{
"name": "底图",
"type": "baidu",
"layer": "img_d"
},
{
"name": "注记",
"type": "baidu",
"layer": "img_z"
}
]
},
{
"pid": 10,
"name": "百度电子",
"icon": "/img/basemaps/bd-vec.png",
"type": "baidu",
"layer": "vec"
},
{
"pid": 10,
"name": "腾讯影像",
"icon": "/img/basemaps/tencent_img.png",
"type": "group",
"layers": [
{
"name": "底图",
"type": "tencent",
"layer": "img_d"
},
{
"name": "注记",
"type": "tencent",
"layer": "img_z"
}
]
},
{
"pid": 10,
"name": "腾讯电子",
"icon": "/img/basemaps/tencent_vec.png",
"type": "tencent",
"layer": "vec"
}
]
}
}
到了这里,关于Mars3D使用教程的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!