vue3中引用cesium,初步搭建

依据项目需求,使用Vue for Cesium ,链接:https://zouyaoji.top/vue-cesium/#/zh-CN

执行

npm install vue-cesium@next --save

全量导入

如果你对打包后的文件大小不是很在乎,那么你可以使用 Vue 的 插件 安装方式配置到全局。

import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'

import VueCesium from 'vue-cesium'
// import Cesium from 'cesium'
import 'vue-cesium/dist/index.css'

createApp(App).use(store).use(router).use(VueCesium).mount('#app')

配置 Vue for Cesium

你可以对 Vue for Cesium 做一些自定义的配置:

import enUS from 'vue-cesium/es/locale/lang/en-us'

app.use(VueCesium, {
  cesiumPath: 'path/to/Cesium.js',
  accessToken: '你的 Cesium Ion 访问令牌',
  locale: enUS // 改成英文
})

使用

 <vc-viewer ref="vcViewer" :animation="animation" :base-layer-picker="baseLayerPicker" :timeline="timeline"
                :fullscreen-button="fullscreenButton" :fullscreen-element="fullscreenElement" :info-box="infoBox"
                :skyAtmosphere="false" :skyBox="false" :scene-mode-picker="true" :show-credit="showCredit"
                @cesium-ready="onCesiumReady" @ready="onViewerReady" @left-click="onLeftClick" @touch-end="onTouchEnd">
                <vc-navigation :offset="offset" @compass-evt="onNavigationEvt" :other-opts="otherOpts"
                    @zoom-evt="onNavigationEvt"></vc-navigation>
                <vc-entity v-model:billboard="billboard" ref="entity" @click="onEntityClick"
                    :position="{ lng: 108, lat: 32 }" :point="point" :label="label">
                    <vc-graphics-billboard ref="billboard"
                        image="https://zouyaoji.top/vue-cesium/favicon.png"></vc-graphics-billboard>
                    <vc-graphics-rectangle :coordinates="[130, 20, 80, 25]" material="green"></vc-graphics-rectangle>
                </vc-entity>
                <vc-layer-imagery>
                    <vc-imagery-provider-tianditu map-style="img_c"
                        token="436ce7e50d27eede2f2929307e6b33c0"></vc-imagery-provider-tianditu>
                </vc-layer-imagery>
                <vc-layer-imagery ref="layerText">
                    <vc-imagery-provider-tianditu map-style="cia_c"
                        token="436ce7e50d27eede2f2929307e6b33c0"></vc-imagery-provider-tianditu>
                </vc-layer-imagery>
            </vc-viewer>
<script>
export default {
    data() {
        return {
            loading: true,
            animation: true,
            timeline: true,
            baseLayerPicker: false,
            fullscreenButton: true,
            infoBox: true,
            showCredit: true,
            fullscreenElement: document.body,
            point: {
                pixelSize: 28,
                color: 'red'
            },
            label: {
                text: 'Hello World',
                pixelOffset: [0, 150]
            },
            billboard: {},
            offset: [50, 25],
            otherOpts: {
                offset: [0, 32],
                position: 'bottom-right'
            }
        }
    },
    watch: {
        timeline(val) {
            this.otherOpts.offset = val ? [0, 30] : this.fullscreenButton ? [30, 5] : [0, 5]
        },
        fullscreenButton(val) {
            if (!this.timeline && !val) {
                this.otherOpts.offset = [0, 5]
            } else if (!this.timeline && val) {
                this.otherOpts.offset = [30, 5]
            }
        }
    },
    mounted() {
        this.$refs.vcViewer.creatingPromise.then(({ Cesium, viewer }) => {
            console.log('viewer is loaded.')
        })
    },
    methods: {
        onViewerReady({ Cesium, viewer }) {
            this.loading = false
            viewer.scene.globe.enableLighting = true
        },
        onCesiumReady(e) {
            console.log(e)
        },
        onNavigationEvt(e) {
            console.log(e)
        },
        onEntityClick(e) {
            console.log(e)
        },
        onLeftClick(e) {
            console.log(e)
        },
        onTouchEnd(e) {
            console.log(e)
        },
        load() {
            this.$refs.vcViewer.load().then(e => {
                console.log(e)
                this.loading = false
            })
        },
        unload() {
            this.$refs.vcViewer.unload().then(e => {
                console.log(e)
                this.loading = true
            })
        },
        reload() {
            this.loading = true
            this.$refs.vcViewer.reload().then(e => {
                console.log(e)
                this.loading = false
            })
        }
    }
}
</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3使用Cesium需要先安装Cesium库,可以通过npm或yarn进行安装: ``` npm install cesium ``` 或者 ``` yarn add cesium ``` 然后在Vue组件引入Cesium: ```javascript import Cesium from 'cesium/Cesium'; import 'cesium/Widgets/widgets.css'; export default { mounted() { // 创建Cesium Viewer const viewer = new Cesium.Viewer('cesiumContainer'); } } ``` 其,`cesiumContainer`是一个DOM元素的ID,代表了Cesium Viewer的容器。 除了基本的Cesium Viewer创建,还可以在Vue组件使用Cesium的其他功能,例如: ```javascript import Cesium from 'cesium/Cesium'; export default { data() { return { viewer: null, // 初始化viewer } }, mounted() { this.viewer = new Cesium.Viewer('cesiumContainer', { animation: false, baseLayerPicker: false, fullscreenButton: false, geocoder: false, homeButton: false, infoBox: false, sceneModePicker: false, selectionIndicator: false, timeline: false, navigationHelpButton: false, navigationInstructionsInitiallyVisible: false, skyBox: false, skyAtmosphere: false, scene3DOnly: true, shouldAnimate: true, imageryProvider: new Cesium.UrlTemplateImageryProvider({ url: 'https://a.tile.openstreetmap.org/{z}/{x}/{y}.png', credit: new Cesium.Credit('OpenStreetMap contributors'), }), terrainProvider: new Cesium.CesiumTerrainProvider({ url: 'https://assets.agi.com/stk-terrain/v1/tilesets/world/tiles', }) }); const entity = this.viewer.entities.add({ position: Cesium.Cartesian3.fromDegrees(-122.19, 46.26), point: { pixelSize: 10, color: Cesium.Color.RED, outlineColor: Cesium.Color.WHITE, outlineWidth: 2, }, label: { text: 'Hello, World!', font: '24px Helvetica', style: Cesium.LabelStyle.FILL_AND_OUTLINE, fillColor: Cesium.Color.YELLOW, outlineColor: Cesium.Color.BLACK, outlineWidth: 2, horizontalOrigin: Cesium.HorizontalOrigin.CENTER, verticalOrigin: Cesium.VerticalOrigin.BOTTOM, pixelOffset: new Cesium.Cartesian2(0, -9), }, }); this.viewer.zoomTo(this.viewer.entities); } } ``` 这段代码创建了一个Cesium Viewer,并在地球上添加了一个实体(点和标签)。这里使用了Cesium的一些常见功能,例如添加实体、设置相机视角等。需要注意的是,在Vue组件使用Cesium时,建议将Cesium实例保存到组件的data,以便在其他方法使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值