|
1 | 1 | # earthsdk-vue-cli-app
|
2 | 2 |
|
| 3 | +## 简介 |
| 4 | + |
| 5 | +通过vue-cli创建项目,然后再基于EarthSDK创建地球。 |
| 6 | + |
| 7 | + |
| 8 | + |
3 | 9 | ## Project setup
|
4 | 10 | ```
|
5 | 11 | npm install
|
@@ -27,3 +33,119 @@ npm run lint
|
27 | 33 |
|
28 | 34 | ### Customize configuration
|
29 | 35 | See [Configuration Reference](https://cli.vuejs.org/config/).
|
| 36 | + |
| 37 | +## 在vue-cli的基础上加入对EarthSDK的支持的方法说明 |
| 38 | + |
| 39 | +1. 加入必须的包 |
| 40 | +``` |
| 41 | +npm install copy-webpack-plugin |
| 42 | +npm install earthsdk |
| 43 | +``` |
| 44 | + |
| 45 | +2. 创建vue.config.js |
| 46 | +```javascript |
| 47 | +// vue.config.js |
| 48 | +const CopyWebpackPlugin = require('copy-webpack-plugin'); |
| 49 | + |
| 50 | +module.exports = { |
| 51 | + configureWebpack: config => { |
| 52 | + const cwp = new CopyWebpackPlugin([ |
| 53 | + { |
| 54 | + from: './node_modules/earthsdk/dist/XbsjCesium', |
| 55 | + to: 'js/earthsdk/XbsjCesium', |
| 56 | + toType: 'dir' |
| 57 | + }, |
| 58 | + { |
| 59 | + from: './node_modules/earthsdk/dist/XbsjEarth', |
| 60 | + to: 'js/earthsdk/XbsjEarth', |
| 61 | + toType: 'dir' |
| 62 | + }, |
| 63 | + ]); |
| 64 | + config.plugins.push(cwp); |
| 65 | + } |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +3. 创建地球组件 EarthComp.vue |
| 70 | +```html |
| 71 | +<template> |
| 72 | + <div style="width: 100%; height: 100%"> |
| 73 | + <div ref="earthContainer" style="width: 100%; height: 100%"></div> |
| 74 | + <div style="position: absolute; left: 18px; top: 18px"> |
| 75 | + <button>{{ message }}</button> |
| 76 | + </div> |
| 77 | + </div> |
| 78 | +</template> |
| 79 | + |
| 80 | +<script> |
| 81 | +// 1 创建Earth的vue组件 |
| 82 | +var EarthComp = { |
| 83 | + data() { |
| 84 | + return { |
| 85 | + message: "页面加载于 " + new Date().toLocaleString(), |
| 86 | + _earth: undefined, // 注意:Earth和Cesium的相关变量放在vue中,必须使用下划线作为前缀! |
| 87 | + _bgImagery: undefined |
| 88 | + }; |
| 89 | + }, |
| 90 | + // 1.1 资源创建 |
| 91 | + mounted() { |
| 92 | + // 1.1.1 创建地球 |
| 93 | + var earth = new XE.Earth(this.$refs.earthContainer); |
| 94 | +
|
| 95 | + // 1.1.2 添加默认地球影像 |
| 96 | + earth.sceneTree.root = { |
| 97 | + children: [ |
| 98 | + { |
| 99 | + czmObject: { |
| 100 | + name: "默认离线影像", |
| 101 | + xbsjType: "Imagery", |
| 102 | + xbsjImageryProvider: { |
| 103 | + createTileMapServiceImageryProvider: { |
| 104 | + url: XE.HTML.cesiumDir + "Assets/Textures/NaturalEarthII", |
| 105 | + fileExtension: "jpg" |
| 106 | + }, |
| 107 | + type: "createTileMapServiceImageryProvider" |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + ] |
| 112 | + }; |
| 113 | +
|
| 114 | + this._earth = earth; |
| 115 | +
|
| 116 | + // 仅为调试方便用 |
| 117 | + window.earth = earth; |
| 118 | + }, |
| 119 | + // 1.2 资源销毁 |
| 120 | + beforeDestroy() { |
| 121 | + // vue程序销毁时,需要清理相关资源 |
| 122 | + this._earth = this._earth && this._earth.destroy(); |
| 123 | + } |
| 124 | +}; |
| 125 | +
|
| 126 | +export default EarthComp; |
| 127 | +</script> |
| 128 | +``` |
| 129 | + |
| 130 | +4. 修改main.js, |
| 131 | + |
| 132 | +需要等待earthsdk载入以后(XE.ready()),再创建vue的示例(new Vue(...)),代码如下: |
| 133 | + |
| 134 | +``` |
| 135 | +// 之前的代码注释掉 |
| 136 | +// new Vue({ |
| 137 | +// render: h => h(App), |
| 138 | +// }).$mount('#app') |
| 139 | +
|
| 140 | +// 修改后的代码 |
| 141 | +function startup() { |
| 142 | + new Vue({ |
| 143 | + render: h => h(App), |
| 144 | + }).$mount('#app') |
| 145 | +} |
| 146 | +
|
| 147 | +// 1 XE.ready()会加载Cesium.js等其他资源,注意ready()返回一个Promise对象。 |
| 148 | +XE.ready().then(startup); |
| 149 | +``` |
| 150 | + |
| 151 | +5. 再改改index.html文件中的css样式等 |
0 commit comments